Skip to content

Commit ea8d883

Browse files
kekoziarmaxim-belkin
authored andcommitted
05-loop.md: use lists when introducing loops
Pull Request: #626
1 parent fa050f1 commit ea8d883

File tree

2 files changed

+74
-72
lines changed

2 files changed

+74
-72
lines changed

_episodes/05-loop.md

Lines changed: 74 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ keypoints:
1515
- "Use `len(thing)` to determine the length of something that contains other values."
1616
---
1717

18-
In the last episode, we wrote Python code that plots values of interest from our first
18+
In the first episode, we wrote Python code that plots values of interest from our first
1919
inflammation dataset (`inflammation-01.csv`), which revealed some suspicious features in it.
2020

2121
![Line graphs showing average, maximum and minimum inflammation across all patients over a 40-day period.](../fig/03-loop_2_0.png)
@@ -24,114 +24,114 @@ We have a dozen data sets right now, though, and more on the way.
2424
We want to create plots for all of our data sets with a single statement.
2525
To do that, we'll have to teach the computer how to repeat things.
2626

27-
An example task that we might want to repeat is printing each character in a
28-
word on a line of its own.
27+
An example task that we might want to repeat is accessing numbers in a list,
28+
which we
29+
will do by printing each number on a line of its own.
2930

3031
~~~
31-
word = 'lead'
32+
odds = [1, 3, 5, 7]
3233
~~~
3334
{: .language-python}
3435

35-
In Python, a string is basically an ordered collection of characters, and every
36-
character has a unique number associated with it -- its index. This means that
37-
we can access characters in a string using their indices.
38-
For example, we can get the first character of the word `'lead'`, by using
39-
`word[0]`. One way to print each character is to use four `print` statements:
36+
In Python, a list is basically an ordered collection of elements, and every
37+
element has a unique number associated with it -- its index. This means that
38+
we can access elements in a list using their indices.
39+
For example, we can get the first number in the list `odds`,
40+
by using `odds[0]`. One way to print each number is to use four `print` statements:
4041

4142
~~~
42-
print(word[0])
43-
print(word[1])
44-
print(word[2])
45-
print(word[3])
43+
print(odds[0])
44+
print(odds[1])
45+
print(odds[2])
46+
print(odds[3])
4647
~~~
4748
{: .language-python}
4849

4950
~~~
50-
l
51-
e
52-
a
53-
d
51+
1
52+
3
53+
5
54+
7
5455
~~~
5556
{: .output}
5657

5758
This is a bad approach for three reasons:
5859

59-
1. **Not scalable**. Imagine you need to print characters of a string that is hundreds
60-
of letters long. It might be easier to type them in manually.
60+
1. **Not scalable**. Imagine you need to print a list that has hundreds
61+
of elements. It might be easier to type them in manually.
6162

62-
2. **Difficult to maintain**. If we want to decorate each printed character with an
63+
2. **Difficult to maintain**. If we want to decorate each printed element with an
6364
asterisk or any other character, we would have to change four lines of code. While
64-
this might not be a problem for short strings, it would definitely be a problem for
65+
this might not be a problem for small lists, it would definitely be a problem for
6566
longer ones.
6667

67-
3. **Fragile**. If we use it with a word that has more characters than what we initially
68-
envisioned, it will only display part of the word's characters. A shorter string, on
69-
the other hand, will cause an error because it will be trying to display part of the
70-
string that doesn't exist.
68+
3. **Fragile**. If we use it with a list that has more elements than what we initially
69+
envisioned, it will only display part of the list's elements. A shorter list, on
70+
the other hand, will cause an error because it will be trying to display elements of the
71+
list that don't exist.
7172

7273
~~~
73-
word = 'tin'
74-
print(word[0])
75-
print(word[1])
76-
print(word[2])
77-
print(word[3])
74+
odds = [1, 3, 5]
75+
print(odds[0])
76+
print(odds[1])
77+
print(odds[2])
78+
print(odds[3])
7879
~~~
7980
{: .language-python}
8081

8182
~~~
82-
t
83-
i
84-
n
83+
1
84+
3
85+
5
8586
~~~
8687
{: .output}
8788

8889
~~~
8990
---------------------------------------------------------------------------
9091
IndexError Traceback (most recent call last)
9192
<ipython-input-3-7974b6cdaf14> in <module>()
92-
3 print(word[1])
93-
4 print(word[2])
94-
----> 5 print(word[3])
93+
3 print(odds[1])
94+
4 print(odds[2])
95+
----> 5 print(odds[3])
9596
96-
IndexError: string index out of range
97+
IndexError: list index out of range
9798
~~~
9899
{: .error}
99100

100101
Here's a better approach:
101102

102103
~~~
103-
word = 'lead'
104-
for char in word:
105-
print(char)
106-
104+
odds = [1, 3, 5, 7]
105+
for num in odds:
106+
print(num)
107107
~~~
108108
{: .language-python}
109109

110110
~~~
111-
l
112-
e
113-
a
114-
d
111+
1
112+
3
113+
5
114+
7
115115
~~~
116116
{: .output}
117117

118-
This is shorter --- certainly shorter than something that prints every character in a
119-
hundred-letter string --- and more robust as well:
118+
This is shorter --- certainly shorter than something that prints every number in a
119+
hundred-letter list --- and more robust as well:
120120

121121
~~~
122-
word = 'oxygen'
123-
for char in word:
124-
print(char)
122+
odds = [1, 3, 5, 7, 9, 11]
123+
for num in odds:
124+
print(num)
125125
~~~
126126
{: .language-python}
127127

128128
~~~
129-
o
130-
x
131-
y
132-
g
133-
e
134-
n
129+
1
130+
3
131+
5
132+
7
133+
9
134+
11
135135
~~~
136136
{: .output}
137137

@@ -145,13 +145,13 @@ for variable in collection:
145145
~~~
146146
{: .language-python}
147147

148-
Using the oxygen example above, the loop might look like this:
148+
Using the example example above, the loop might look like this:
149149

150-
![Loop variable 'char' being assigned the value of each character in the word 'oxygen' in turn and then being printed](../fig/loops_image.png)
150+
![Loop variable 'num' being assigned the value of each element in the list `odds` in turn and then being printed](../fig/05-loops_image_num.png)
151151

152-
where each character (`char`) in the variable `word` is looped through and printed one character
153-
after another. The numbers in the diagram denote which loop cycle the character was printed in (1
154-
being the first loop, and 6 being the final loop).
152+
where each number (`num`) in the variable `odds` is looped through and printed one number after
153+
another. The other numbers in the diagram denote which loop cycle the number was printed in (1
154+
being the first loop cycle, and 6 being the final loop cycle).
155155

156156
We can call the [loop variable]({{ page.root }}/reference.html#loop-variable) anything we like, but
157157
there must be a colon at the end of the line starting the loop, and we must indent anything we
@@ -162,24 +162,26 @@ of the loop body (e.g. `end for`); what is indented after the `for` statement be
162162
> ## What's in a name?
163163
>
164164
>
165-
> In the example above, the loop variable was given the name `char` as a mnemonic;
166-
> it is short for 'character'. We can choose any name we want for variables.
167-
> We can even call our loop variable `banana`, as long as we use this name consistently:
165+
> In the example above, the loop variable was given the name `num` as a mnemonic;
166+
> it is short for 'number'.
167+
> We can choose any name we want for variables. We might just as easily have chosen the name
168+
> `banana` for the loop variable, as long as we use the same name when we invoke the variable inside
169+
> the loop:
168170
>
169171
> ~~~
170-
> word = 'oxygen'
171-
> for banana in word:
172+
> odds = [1, 3, 5, 7, 9, 11]
173+
> for banana in odds:
172174
> print(banana)
173175
> ~~~
174176
> {: .language-python}
175177
>
176178
> ~~~
177-
> o
178-
> x
179-
> y
180-
> g
181-
> e
182-
> n
179+
> 1
180+
> 3
181+
> 5
182+
> 7
183+
> 9
184+
> 11
183185
> ~~~
184186
> {: .output}
185187
>

fig/05-loops_image_num.png

32.2 KB
Loading

0 commit comments

Comments
 (0)