Lesson 7 of 30
Lesson 07 — Lists of pets, snacks, and scores
Title: Keep many values in one place
Description: Use lists to store pets, snacks, scores, and todo items.
Why it matters for kids: A list is like a backpack for code: it can hold many things.
1. First list
pets = ["cat", "dog", "hamster"]
print(pets)
print(pets[0])
print(pets[1])
print(pets[2])List positions start at 0.
2. Add to a list
snacks = ["apple", "cookie"]
snacks.append("banana")
snacks.append("sandwich")
print(snacks)append() puts a new item at the end.
3. Loop through a list
games = ["tag", "chess", "soccer"]
for game in games:
print(f"I like {game}.")4. Score list
scores = [10, 20, 15]
total_score = 0
for score in scores:
total_score = total_score + score
print("Total score:", total_score)5. Easy examples
colors = ["red", "green", "blue"]
print(f"The first color is {colors[0]}.")
print(f"The last color is {colors[-1]}.")todo_items = ["brush teeth", "read", "code"]
for item in todo_items:
print(f"Todo: {item}")animals = []
animals.append("fox")
animals.append("owl")
animals.append("bear")
print(animals)6. Mini challenge
Click each example to open it.
Example 1
Create a list of three pets.
Example 2
Print the first pet.
Example 3
Print the last pet.
Example 4
Add one snack with append.
Example 5
Loop through three games.
Example 6
Create a list of three scores.
Example 7
Add all scores together.
Example 8
Create an empty list and add two animals.
Example 9
Print every food in a food list.
Example 10
Change the second item in a list.