Kids Coding
Lists of Things
A list holds many items in order. Add, read, and loop through them.
KidsIntermediateLists of Things
Many things in one box
A list holds items in order, inside square brackets, separated by commas.
A list of pets
pets = ["cat", "dog", "fox"]
print(pets)
print(pets[0])
print(pets[2])The first item is index 0. pets[0] is cat. Counting from zero feels odd once, then it is normal.
Loop through a list
Say hello to each
pets = ["cat", "dog", "fox"]
for pet in pets:
print("Hello, " + pet)Add an item
append
pets = ["cat", "dog"]
pets.append("fox")
print(pets)
print("Count:", len(pets))Real life
A backpack before school.
Many items, one bag. A list is the bag. Index 0 is the first thing you packed.
Backpack
bag = ["book", "lunch", "water"]
print("First in:", bag[0])
bag.append("hoodie")
print("Packed:", len(bag), "things")
for item in bag:
print("-", item)Change one word. Run it. That is how this idea shows up outside the lesson.
Try it
Exercise 1: Make a list of three foods. Print the second food (index 1).
Show solution
foods = ["rice", "noodles", "bread"]
print(foods[1])