← All missions

πŸ” Finding Things

πŸŽ’ Organizing Stuff with Lists

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 / 3
team = ["Bolt", "Nova", "Sky"]
print(team[0])
0: Bolt
1: Nova
2: Sky

Positions start at zero. team[0] gets Bolt.

3) Run the code & terminal output

Code Lab 🐍Playground β€” experiment freely!
Hero Terminal

Press β–Ά RUN and your program's messages appear here!

Equipment Vault
Equipment Vault
Your superhero companion

Captain Py

Our next adventure is yours to create.