1) What do I need to do?
π― Your mission: Most wanted
Make a list of 5 villains.
Print the first villain using [0], the last using [-1], and how many there are using len().
2) What do I need to know?
Positions start at 0
Every item in a list has a position number called an index.
Python starts counting at 0! So the first item is team[0].
team[-1] is a shortcut for the last item.
len(team) tells you how many items are in the list.
π§° Your toolkit for this mission
Everything you need to know to finish this lesson on your own:
Index
The position number of an item. Counting starts at 0!
How to write it: team[0] is the first item, team[1] the second.
villains = ["Rex", "Vex", "Hex"] print(villains[0]) # Rex
Negative index
Count from the end.
How to write it: [-1] is the last item, [-2] the one before.
print(villains[-1]) # Hex
len()
How many items there are.
How to write it: len(villains)
print(len(villains)) # 3
Replacing an item
Put a new value at a position.
How to write it: list[index] = new_value. The middle of 3 items is index 1.
team[1] = "Nova" print(team)
Think about it
Look at this code. What happens next, and why? Say it in your own words.
print(len(team))
Watch & explore βΆ
Power-up: Positions start at 0
See it happen
1 / 3team = ["Bolt", "Nova", "Sky"] print(team[0])
Positions start at zero. team[0] gets Bolt.
3) Run the code & terminal output
Press βΆ RUN and your program's messages appear here!


Captain Py
Our next adventure is yours to create.
