Lesson 7 of 30
Lists and Tuples
Creating, indexing, slicing, and iterating over ordered collections.
Lists
A list is an ordered, mutable collection that can hold items of any type:
fruits = ["apple", "banana", "cherry"]
numbers = [10, 20, 30, 40]
mixed = ["Alice", 25, True, 3.14]
Accessing and Modifying
print(fruits[0]) # apple
print(fruits[-1]) # cherry
fruits[1] = "blueberry"
print(fruits) # ['apple', 'blueberry', 'cherry']
List Methods
fruits.append("date") # add to end
fruits.insert(1, "avocado") # insert at index
fruits.remove("cherry") # remove by value
popped = fruits.pop() # remove and return last item
fruits.sort() # sort in place
fruits.reverse() # reverse in place
print(len(fruits)) # number of items
Tuples
Tuples are like lists but immutable — once created they cannot be changed. They use parentheses:
point = (3, 7)
rgb = (255, 128, 0)
print(point[0]) # 3
x, y = point # unpacking
Iterating Over a List
scores = [88, 72, 95, 60]
for score in scores:
print(f"Score: {score}")
# With index
for i, score in enumerate(scores):
print(f"{i+1}. {score}")