← Back to course
1/6
Week 05 · Lists

Monday

Six variables, one problem
// Keeping track of many values at once
⏱ about 20 min

Monday: Six Variables, One Problem

Beacon now reads the water level every four hours. The greenhouse team wants to see the whole day at once.

Comet gets to work. She makes reading1, reading2, reading3, reading4, reading5 and reading6.

"Six variables, six readings," she says proudly.

Then the team asks for a reading every hour instead. Comet stares at her page. "That is twenty-four variables."

Wren taps the page. "What do you notice about all those names?"

"They are all the same," Comet says slowly, "except for the number at the end."

Nova projects a single row of glowing boxes. "Interesting," she says.

"What if one name could hold the whole row?"

This week's driving question

How does a program keep track of many values at once?

This week you will store Beacon's readings in a list, find the highest and lowest, and search for a value.

What is a list?

A list is an ordered sequence of elements. An element is one value in the list.

Each element has a unique index, its position number. On the AP reference sheet, the first index is 1.

Some programming languages call a list an array.

A variable holds one value at a time. But that one value can be a list, which holds many values.

readings ← [62, 58, 54, 48, 70, 68]
Index123456
Element625854487068

We write readings[3] to mean the element at index 3. Here, readings[3] is 54.

READ THE LIST
  • Read the question.
  • Tap your answer.
What is readings[1]?
What is readings[5]?
At which index is the reading 48?
How many elements does readings hold?

Strings are sequences too

A string is an ordered sequence of characters. The string "basil" has five characters in order.

A list can hold strings. Beacon could keep a list of plant names next to its list of readings.

plants ← ["tomato", "basil", "lettuce", "bean"]
What is plants[2]?
What is plants[4]?
One value inside a list is called an ____.
StatementTrue or false?
A list is an ordered sequence of elements.?
On the AP reference sheet, the first index of a list is 0.?
A variable can hold a whole list as its one value.?
Some languages call a list an array.?
WHY THIS EXERCISEThese facts are the ground rules for every list you will write this week.
On paper, sketch Beacon's readings list as a row of six boxes. Write each index above its box and each element inside.
Try it
Write a list of five things you can see right now, in order from left to right.
Number them from 1. Which item is at index 3?

Strong start, developer. Tomorrow you will walk through a list one element at a time.