← Back to course
Code Crew G4 / Week 02 / Monday
1/6
Week 02 Β· Lists

Monday

One name, many values
// Many values, one name
⏱ about 15 min

Monday: One name, many values

A cargo pod docks with a soft thump. Rocket starts typing a variable for each thing inside it: cargo1, cargo2, cargo3, cargo4. By cargo9, his fingers are tired.

"There must be a faster way," he sighs.

Raven floats over to read the screen. "What do you notice about all these names?" she asks. "They are all cargo," Rocket says. "Only the number changes."

Nova draws a long glowing shelf in the air. "Would you like a hint? One name can hold a whole row of values."

Make a list

A list keeps many values in order under one name.

Square brackets [ ] hold the list. Commas go between the items.

cargo = ["water", "oats", "tools"]
fuel = [30, 45, 20]
print(cargo)
print(fuel)
The screen shows
['water', 'oats', 'tools']
[30, 45, 20]
  • Python shows a list with its brackets and commas.
  • Python shows strings in a list with single quote marks. Single and double quotes both work in Python.
  • A list can hold strings, numbers or both.
How many items are in this list? ["rope", "tape", "gloves", "lamps"]
WHY THIS EXERCISECounting items is the first step to using a list well.
ListDoes it hold exactly 3 items?
[4, 8, 15]?
["oats", "rice"]?
["a, b, c"]?
["red", "blue", "green"]?
WHY THIS EXERCISE"a, b, c" is one string, so that list has only one item. The quote marks decide.
crew = ["Rocket", "Raven"]
print(crew)
WHAT SHOWS ON THE SCREEN?
  • Read the question.
  • Tap your answer.
Look at the program above. What does print(crew) show?
Try it
Look around your room.
Write a list of four things you see, in Python style with brackets and commas.

Practice lab: read the lists

kit = ["rope", "lamp", "tape"]
nums = [12, 7, 30, 5]
print(kit)
print(nums)
How many items are in kit?
How many items are in nums?
Which list holds strings?
BUILD A LIST
  • Read the question.
  • Tap your answer.
Which line makes a list of two tools?
What goes between the items in a list?
How many items are in ["a, b", "c"]?
StatementTrue or false?
[5, 10] holds 2 items.?
A list can only hold numbers.?
["one"] holds 1 item.?
[ ] holds 1 item.?
WHY THIS EXERCISEA list can hold strings, numbers or nothing at all.
How many items are in this list? [3, "rope", 8, "tape", 1]
WHY THIS EXERCISEA list can mix numbers and strings, and every value is one item.

Great job! You made your first lists. Tomorrow you will reach into a list and grab one item.