Skip to content

Commit 51742ce

Browse files
biokcbmaxim-belkin
authored andcommitted
05-loop.md: replace string loops with lists
Pull Request: #634
1 parent 3be84ce commit 51742ce

File tree

2 files changed

+51
-32
lines changed

2 files changed

+51
-32
lines changed

_episodes/05-loop.md

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -193,65 +193,66 @@ Here's another loop that repeatedly updates a variable:
193193
194194
~~~
195195
length = 0
196-
for vowel in 'aeiou':
196+
names = ['Curie', 'Darwin', 'Turing']
197+
for value in names:
197198
length = length + 1
198-
print('There are', length, 'vowels')
199+
print('There are', length, 'names in the list.')
199200
~~~
200201
{: .language-python}
201202
202203
~~~
203-
There are 5 vowels
204+
There are 3 names in the list.
204205
~~~
205206
{: .output}
206207
207208
It's worth tracing the execution of this little program step by step.
208-
Since there are five characters in `'aeiou'`,
209-
the statement on line 3 will be executed five times.
209+
Since there are three names in `names`,
210+
the statement on line 4 will be executed three times.
210211
The first time around,
211212
`length` is zero (the value assigned to it on line 1)
212-
and `vowel` is `'a'`.
213+
and `value` is `Curie`.
213214
The statement adds 1 to the old value of `length`,
214215
producing 1,
215216
and updates `length` to refer to that new value.
216217
The next time around,
217-
`vowel` is `'e'` and `length` is 1,
218+
+`value` is `Darwin` and `length` is 1,
218219
so `length` is updated to be 2.
219-
After three more updates,
220-
`length` is 5;
221-
since there is nothing left in `'aeiou'` for Python to process,
220+
After one more update,
221+
`length` is 3;
222+
since there is nothing left in `names` for Python to process,
222223
the loop finishes
223-
and the `print` statement on line 4 tells us our final answer.
224+
and the `print` function on line 5 tells us our final answer.
224225
225226
Note that a loop variable is a variable that's being used to record progress in a loop.
226227
It still exists after the loop is over,
227228
and we can re-use variables previously defined as loop variables as well:
228229
229230
~~~
230-
letter = 'z'
231-
for letter in 'abc':
232-
print(letter)
233-
print('after the loop, letter is', letter)
231+
name = 'Rosalind'
232+
for name in ['Curie', 'Darwin', 'Turing']:
233+
print(name)
234+
print('after the loop, name is', name)
234235
~~~
235236
{: .language-python}
236237
237238
~~~
238-
a
239-
b
240-
c
241-
after the loop, letter is c
239+
Curie
240+
Darwin
241+
Turing
242+
after the loop, name is Turing
242243
~~~
243244
{: .output}
244245
245-
Note also that finding the length of a string is such a common operation
246+
Note also that finding the length of an object is such a common operation
246247
that Python actually has a built-in function to do it called `len`:
247248
248249
~~~
249-
print(len('aeiou'))
250+
print(len([0, 1, 2, 3]))
250251
~~~
251252
{: .language-python}
252253
253254
~~~
254-
5
255+
4
255256
~~~
256257
{: .output}
257258
@@ -351,20 +352,19 @@ so we should always use it when we can.
351352
> {: .solution}
352353
{: .challenge}
353354
354-
> ## Reverse a String
355+
> ## Summing a list
355356
>
356-
> Knowing that two strings can be concatenated using the `+` operator,
357-
> write a loop that takes a string
358-
> and produces a new string with the characters in reverse order,
359-
> so `'Newton'` becomes `'notweN'`.
357+
> Write a loop that calculates the sum of elements in a list
358+
> by adding each element and printing the final value,
359+
> so `[124, 402, 36]` prints 562
360360
>
361361
> > ## Solution
362362
> > ~~~
363-
> > newstring = ''
364-
> > oldstring = 'Newton'
365-
> > for char in oldstring:
366-
> > newstring = char + newstring
367-
> > print(newstring)
363+
> > numbers = [124, 402, 36]
364+
> > summed = 0
365+
> > for num in numbers:
366+
> > summed = summed + num
367+
> > print(summed)
368368
> > ~~~
369369
> > {: .language-python}
370370
> {: .solution}

_extras/extra_exercises.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,25 @@ or not (yet) added to the main lesson.
7676
> {: .solution}
7777
{: .challenge}
7878
79+
> ## Reverse a String
80+
>
81+
> Knowing that two strings can be concatenated using the `+` operator,
82+
> write a loop that takes a string
83+
> and produces a new string with the characters in reverse order,
84+
> so `'Newton'` becomes `'notweN'`.
85+
>
86+
> > ## Solution
87+
> > ~~~
88+
> > newstring = ''
89+
> > oldstring = 'Newton'
90+
> > for char in oldstring:
91+
> > newstring = char + newstring
92+
> > print(newstring)
93+
> > ~~~
94+
> > {: .language-python}
95+
> {: .solution}
96+
{: .challenge}
97+
7998
> ## Fixing and Testing
8099
> From: "Defensive Programming"
81100
>

0 commit comments

Comments
 (0)