← Back to course
Code Crew G4 / Week 02 / Tuesday
2/6
Week 02 Β· Lists

Tuesday

Counting from zero
// Many values, one name
⏱ about 15 min

Tuesday: Counting from zero

"Rocket, can you pass me the first item on the cargo list?" Raven asks. Rocket opens the list and grabs spot number one. Out floats a bag of oats.

"I wanted the water," Raven laughs. "What do you notice about how Python numbers the spots?"

Rocket frowns at the screen. The water is at the very front. So why did spot one give him oats?

Nova blinks. "Interesting. Would you like a hint? Python starts counting at a number smaller than one."

Every item has an index

Each item in a list has a spot number called an index.

Python starts counting at 0. So the first item is at index 0.

To get one item, write the list name, then the index in square brackets.

cargo = ["water", "oats", "tools", "lamps"]
print(cargo[0])
print(cargo[2])
Index0123
Itemwateroatstoolslamps
The screen shows
water
tools

Rocket asked for spot 1, so he got the second item. That is why the oats floated out.

cargo[1] gives
cargo[3] gives
Which index gets "tools" from the cargo list?
WHY THIS EXERCISEThe index is always one less than the place you would say out loud.
launches = ["Mars run", "Moon hop", "Ring tour"]
print(launches[1])
GRAB ONE ITEM
  • Read the question.
  • Tap your answer.
What does the program above show?
Which line shows "Ring tour"?
Try it
Line up four objects on a table.
Put a scrap of paper under each one with 0, 1, 2 and 3.
Ask a grown-up to call out an index. Hand over that object.

Practice lab: grab by index

moons = ["Pip", "Zed", "Ula", "Bix"]
print(moons[0])
print(moons[3])
print(moons[1])
What does line 2 show?
What does line 3 show?
What does line 4 show?
Which index gets "Ula"?
COUNT FROM ZERO
  • Read the question.
  • Tap your answer.
Index 0 is which item?
colors = ["red", "blue", "gold"]. What is colors[1]?
Which index is the fourth item?
Statement about the moons listTrue or false?
moons[0] is "Pip".?
moons[4] is "Bix".?
moons[2] is "Ula".?
moons[1] is "Pip".?
WHY THIS EXERCISEFour items use indexes 0 to 3. There is no index 4 in this list.
A list holds ["Mon", "Tue", "Wed", "Thu"]. Which index gets "Thu"?
WHY THIS EXERCISEThe fourth item sits at index 3.

Counting from zero feels odd at first, and you did it! Tomorrow you will count how many items a list holds.

← Monday