Table of Contents
1. Introduction to Lists | 1. Introduction to Tuples |
– Definition and characteristics | – Definition and characteristics |
– Mutable nature | – Immutable nature |
2. Basic Operations | 2. Creating and Accessing Tuples |
– Creating lists | – Tuple creation syntax |
– Accessing elements | – Indexing and accessing elements |
| 3. Tuple Packing and Unpacking |
3. Common Methods | – Explanation and examples |
– insert() and remove() | 4. Common Operations |
– pop() and clear() | – Concatenating tuples |
| – Converting tuples to lists and vice versa |
– Using for loops | 5. Iterating over Tuples |
– List comprehensions | – Using for loops |
5. Slicing and Indexing | 6. Advantages of Tuples |
– Indexing basics | – Performance benefits |
– Slicing syntax and examples | – Use cases |
6. List Manipulation Techniques | 7. Comparison with Lists |
– Sorting lists | – Differences in mutability and usage |
– Concatenating lists | 8. Best Practices |
| – When to use tuples over lists |
7. Nested Lists | – Maintaining data integrity |
– Accessing elements in nested lists | |
8. Best Practices | – Recap of key differences |
– Efficiency considerations | – Recommendations for practical usage |
Lists in Python
Definition: Lists are ordered collections of items, mutable (modifiable after creation), and can contain elements of different data types.
# Creating a list
my_list = [1, 2, 3, 'a', 'b', 'c']
# Accessing elements
print(my_list[0]) # Output: 1
print(my_list[3]) # Output: 'a'
# Modifying elements
my_list[1] = 10
print(my_list) # Output: [1, 10, 3, 'a', 'b', 'c']
# Adding elements
my_list.append('d')
print(my_list) # Output: [1, 10, 3, 'a', 'b', 'c', 'd']
# Removing elements
removed_item = my_list.pop(2)
print(my_list) # Output: [1, 10, 'a', 'b', 'c', 'd']
print(removed_item) # Output: 3
# Iterating over a list
for item in my_list:
print(item)
# List slicing
print(my_list[1:4])
Output: