@@ -21,21 +21,23 @@ keypoints:
2121---
2222## A list stores many values in a single structure.
2323
24- * Doing calculations with a hundred variables called ` pressure_001 ` , ` pressure_002 ` , etc.,
24+ * Scenario: You have set up an Arduino to do temperature measurements
25+ in a storage room for rare books.
26+ * Doing calculations with a hundred variables called ` temperature_001 ` , ` temperature_002 ` , etc.,
2527 would be at least as slow as doing them by hand.
2628* Use a * list* to store many values together.
2729 * Contained within square brackets ` [...] ` .
2830 * Values separated by commas ` , ` .
2931* Use ` len ` to find out how many values are in a list.
3032
3133~~~
32- pressures = [0.273, 0.275, 0.277, 0.275, 0.276 ]
33- print('pressures :', pressures )
34- print('length:', len(pressures ))
34+ temperatures = [17.3, 17.5, 17.7, 17.5, 17.6 ]
35+ print('temperatures :', temperatures )
36+ print('length:', len(temperatures ))
3537~~~
3638{: .python}
3739~~~
38- pressures : [0.273, 0.275, 0.277, 0.275, 0.276 ]
40+ temperatures : [17.3, 17.5, 17.7, 17.5, 17.6 ]
3941length: 5
4042~~~
4143{: .output}
@@ -45,13 +47,13 @@ length: 5
4547* Just like strings.
4648
4749~~~
48- print('zeroth item of pressures :', pressures [0])
49- print('fourth item of pressures :', pressures [4])
50+ print('zeroth item of temperatures :', temperatures [0])
51+ print('fourth item of temperatures :', temperatures [4])
5052~~~
5153{: .python}
5254~~~
53- zeroth item of pressures: 0.273
54- fourth item of pressures: 0.276
55+ zeroth item of temperatures: 17.3
56+ fourth item of temperatures: 17.6
5557~~~
5658{: .output}
5759
@@ -60,12 +62,12 @@ fourth item of pressures: 0.276
6062* Use an index expression on the left of assignment to replace a value.
6163
6264~~~
63- pressures [0] = 0.265
64- print('pressures is now:', pressures )
65+ temperatures [0] = 16.5
66+ print('temperatures is now:', temperatures )
6567~~~
6668{: .python}
6769~~~
68- pressures is now: [0.265, 0.275, 0.277, 0.275, 0.276 ]
70+ temperatures is now: [16.5, 17.5, 17.7, 17.5, 17.6 ]
6971~~~
7072{: .output}
7173
@@ -74,16 +76,15 @@ pressures is now: [0.265, 0.275, 0.277, 0.275, 0.276]
7476* Use ` list_name.append ` to add items to the end of a list.
7577
7678~~~
77- primes = [2, 3, 5]
78- print('primes is initially:', primes)
79- primes.append(7)
80- primes.append(9)
81- print('primes has become:', primes)
79+ print('temperatures is initially:', temperatures)
80+ temperatures.append(17.9)
81+ temperatures.append(18.2)
82+ print('temperatures has become:', temperatures)
8283~~~
8384{: .python}
8485~~~
85- primes is initially: [2, 3, 5 ]
86- primes has become: [2, 3, 5, 7, 9 ]
86+ temperatures is initially: [16.5, 17.5, 17.7, 17.5, 17.6 ]
87+ temperatures has become: [16.5, 17.5, 17.7, 17. 5, 17.6, 17.9, 18.2 ]
8788~~~
8889{: .output}
8990
@@ -93,25 +94,6 @@ primes has become: [2, 3, 5, 7, 9]
9394 * Deliberately resembles the way we refer to things in a library.
9495* We will meet other methods of lists as we go along.
9596 * Use ` help(list) ` for a preview.
96- * ` extend ` is similar to ` append ` , but it allows you to combine two lists. For example:
97-
98- ~~~
99- teen_primes = [11, 13, 17, 19]
100- middle_aged_primes = [37, 41, 43, 47]
101- print('primes is currently:', primes)
102- primes.extend(teen_primes)
103- print('primes has now become:', primes)
104- primes.append(middle_aged_primes)
105- print('primes has finally become:', primes)
106- ~~~
107- {: .python}
108- ~~~
109- primes is currently: [2, 3, 5, 7, 9]
110- primes has now become: [2, 3, 5, 7, 9, 11, 13, 17, 19]
111- primes has finally become: [2, 3, 5, 7, 9, 11, 13, 17, 19, [37, 41, 43, 47]]
112- ~~~
113- {: .output}
114- Note that while ` extend ` maintains the "flat" structure of the list, appending a list to a list makes the result two-dimensional.
11597
11698## Use ` del ` to remove items from a list entirely.
11799
0 commit comments