Last time, we saw how lists like planets = ["Mercury", "Venus", "Earth"]
can hold multiple values together in order.
But how do we get just one specific item out of the list, like "Venus"?
Engagement Message
Any ideas on how we might pinpoint an item?
Each item in a list has a specific position, called an index. Think of it like a numbered slot.
Python starts numbering these positions from zero, not one! This is called zero-based indexing.
Engagement Message
So, what index number do you think the first item in a list has?
That's right, the first item is at index 0
. The second item is at index 1
, the third at index 2
, and so on.
To access an item, you use the list variable followed by the index in square brackets []
.
Engagement Message
If planets = ["Mercury", "Venus", "Earth"]
, what value does planets[0]
give?
Using our planets
list:
planets[0]
gives
gives
etc.
