Lesson 9: Lists and Indexing
Topics Covered:
- What are lists and how to create them
- Accessing list items by index
- Modifying lists (adding/removing items)
- Looping through lists
What is a List?
A list is a collection of items stored in a specific order:
fruits = ["apple", "banana", "cherry"]
Accessing Items by Index
Indexing starts at 0, so the first item is at index 0:
print(fruits[0]) # apple
print(fruits[2]) # cherry
Changing Items in a List
fruits[1] = "blueberry"
print(fruits) # ['apple', 'blueberry', 'cherry']
Adding Items
fruits.append("orange")
print(fruits) # ['apple', 'blueberry', 'cherry', 'orange']
Removing Items
fruits.remove("apple")
print(fruits) # ['blueberry', 'cherry', 'orange']
Length of a List
Use len()
to find how many items are in a list:
print(len(fruits)) # 3
Looping Through a List
for fruit in fruits:
print("I like", fruit)
Activity
- Create a list of your favorite games or movies.
- Print each item with a message.
- Add a new item and remove one.
games = ["Minecraft", "Mario", "Zelda"]
for game in games:
print("I love playing", game)
games.append("Among Us")
games.remove("Mario")
print("Updated list:", games)