The launch schedule keeps growing. Every time a pilot signs up, Raven adds another time to the list. "How many launches are there now?" Rocket asks.
He starts counting with his finger, but the list scrolls off the screen. "Seven? No, wait. Eight?"
Raven smiles. "What do you notice? We keep counting by hand. Could Python count for us?"
Nova lights up her wings. "Would you like a hint? Python has a short command that measures a list. Its name is short for length."
len is short for length. It counts how many items are in a list.
schedule = ["0800", "1100", "1400", "1700", "2000"] print(len(schedule)) print(schedule[4])
| The screen shows |
|---|
| 5 |
| 2000 |
tools = ["drill", "tape", "wrench", "rope", "lamp", "clamp"] print(len(tools))
| A list has 4 items. | True or false? |
|---|---|
| len gives 4 | ? |
| The first index is 1 | ? |
| Index 3 is the last item | ? |
| Index 4 is the last item | ? |
pilots = ["Ada", "Ben", "Cy", "Dee"] print(len(pilots)) print(pilots[len(pilots) - 1])
| Statement | True or false? |
|---|---|
| len counts the items in a list. | ? |
| len([ ]) gives 1. | ? |
| A list with 6 items has a last index of 5. | ? |
| len gives the last index. | ? |
Super counting! Tomorrow you will find the last item fast and change items in a list.