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?"
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.
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]
| Index | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|
| Element | 62 | 58 | 54 | 48 | 70 | 68 |
We write readings[3] to mean the element at index 3. Here, readings[3] is 54.
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"]
| Statement | True 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. | ? |
Strong start, developer. Tomorrow you will walk through a list one element at a time.