The fleet glides into a new star cluster. Thousands of points of light sparkle in every window.
"Our job is to map it," Rocket says. "I will start the star catalog right now!" He types five names into a list.
Nova projects the list in glowing blue letters. Rocket points at the first name. "That one is star number 1."
Raven tilts her head. "What do you notice about the spot numbers Nova shows?"
Nova blinks her cyan lights. "Here is a hint. Count the way Python counts."
A list holds many values under one name. Square brackets wrap the list. Commas go between the values.
Each value sits in a spot called an index. Python starts counting spots at 0, not 1.
stars = ["Ember", "Frost", "Glow", "Halo", "Iris"] print(stars[0]) print(stars[3]) print(len(stars)) print(stars[-1])
Output: Ember Halo 5 Iris
| Index | 0 | 1 | 2 | 3 | 4 |
|---|---|---|---|---|---|
| Star | Ember | Frost | Glow | Halo | Iris |
The append() tool adds a new item to the end of a list. The list grows by one.
stars = ["Ember", "Frost", "Glow"]
stars.append("Juno")
print(len(stars))
print(stars[-1]) | Statement about stars = ["Ember", "Frost", "Glow", "Halo", "Iris"] | True or false? |
|---|---|
| stars[0] is "Ember". | ? |
| stars[5] is "Iris". | ? |
| len(stars) is 5. | ? |
| stars[-1] is "Iris". | ? |
probes = ["Kit", "Ivo", "Sol"]
probes.append("Bea")
probes[1] = "Max"
print(probes)
print(len(probes))
print(probes[-2]) | Statement about probes | True or false? |
|---|---|
| probes[1] = "Max" replaced "Ivo". | ? |
| len(probes) is 3 at the end. | ? |
| probes[-1] is "Bea". | ? |
| append can only add numbers. | ? |
Great start, Crew Engineer! Lists are the backbone of the star catalog. Tomorrow we loop through a list and add things up.