Py4inf 05 Iterations
Py4inf 05 Iterations
Chapter 5
Unless otherwise noted, the content of this course material is licensed under a Creative
Commons Attribution 3.0 License.
http://creativecommons.org/licenses/by/3.0/.
Copyright 2010- Charles Severance
n = 5
No
Yes
Repeated Steps
Program:
n = 5
while n > 0 :
print n
n = n 1
print 'Blastoff!'
print n
Output:
5
print n
4
3
n = n -1
2
1
Blastoff!
0
print 'Blastoff'
Loops (repeated steps) have iteration variables that
change each time through a loop. Often these
iteration variables go through a sequence of numbers.
n > 0 ?
n = 5
No
Yes
n > 0 ?
print 'Lather'
print 'Rinse'
An Infinite Loop
n = 5
while n > 0 :
print 'Lather
print 'Rinse'
print 'Dry off!'
n = 0
No
Yes
n > 0 ?
print 'Lather'
print 'Rinse'
Another Loop
n = 0
while n > 0 :
print 'Lather
print 'Rinse'
print 'Dry off!'
It is like a loop test that can happen anywhere in the body of the loop
while True:
line = raw_input('> ')
if line == 'done' :
break
print line
print 'Done!'
It is like a loop test that can happen anywhere in the body of the loop
while True:
line = raw_input('> ')
if line == 'done' :
break
print line
print 'Done!'
while True:
line = raw_input('> ')
if line == 'done' :
break
print line
print 'Done!'
No
True ?
Yes
....
break
...
http://en.wikipedia.org/wiki/Transporter_(Star_Trek)
print 'Done'
No
while True:
line = raw_input('> )
if line[0] == '#' :
continue
if line == 'done' :
break
print line
print 'Done!'
True ?
Yes
....
continue
...
print 'Done'
Indefinite Loops
While loops are called "indefinite loops" because they keep going until
a logical condition becomes False
The loops we have seen so far are pretty easy to examine to see if
they will terminate or if they will be "infinite loops"
Definite Loops
Quite often we have a list of items of the lines in a file - effectively a
finite set of things
We can write a loop to run the loop once for each of the items in a
set using the Python for construct
These loops are called "definite loops" because they execute an exact
number of times
5
4
3
2
1
Blastoff!
No
Done?
Move i ahead
print i
for i in [5, 4, 3, 2, 1] :
print i
print 'Blastoff!'
5
4
3
2
1
Blastoff!
Looking at In...
The iteration variable
Iteration variable
Five-element sequence
for i in [5, 4, 3, 2, 1] :
print i
Yes
No
Done?
Move i ahead
print i
iterates
though the sequence (ordered set)
i = 5
Yes
No
print i
Done?
Move i ahead
print i
i = 4
print i
i = 3
print i
i = 2
for i in [5, 4, 3, 2, 1] :
print i
print i
i = 1
print i
Definite Loops
Quite often we have a list of items of the lines in a file - effectively a
finite set of things
We can write a loop to run the loop once for each of the items in a
set using the Python for construct
These loops are called "definite loops" because they execute an exact
number of times
Loop Idioms
What We Do in Loops
Note: Even though these examples are simple, the
patterns apply to all kinds of loops
The trick is
knowing something
about the whole loop when you
are stuck writing code that only
sees one entry at a time
$ python basicloop.py
Before
9
41
12
3
74
15
After
41
12
9
largest_so_far
-1
3
41
74
74
15
Counting in a Loop
$ python countloop.py
Before 0
1 9
2 41
3 12
4 3
5 74
6 15
After 6
To count how many times we execute a loop we introduce a counter
variable that starts at 0 and we add one to it each time through the loop.
zork = 0
print 'Before', zork
for thing in [9, 41, 12, 3, 74, 15] :
zork = zork + 1
print zork, thing
print 'After', zork
Summing in a Loop
zork = 0
print 'Before', zork
for thing in [9, 41, 12, 3, 74, 15] :
zork = zork + thing
print zork, thing
print 'After', zork
$ python countloop.py
Before 0
9 9
50 41
62 12
65 3
139 74
154 15
After 154
$ python averageloop.py
Before 0 0
1 9 9
2 50 41
3 62 12
4 65 3
5 139 74
6 154 15
After 6 154 25
Filtering in a Loop
print 'Before
for value in [9, 41, 12, 3, 74, 15] :
if value > 20:
print 'Large number',value
print 'After'
$ python search1.py
Before
Large number 41
Large number 74
After
$ python search1.py
Before False
False 9
False 41
False 12
True 3
True 74
True 15
After True
If we just want to search and know if a value was found - we use a variable that starts
at False and is set to True as soon as we find what we are looking for.
41
12
3
smallest_so_far
-1
74
15
41
12
3
largest_so_far
None
9
3
74
15
$ python smallest.py
Before
9 9
9 41
9 12
3 3
3 74
3 15
After 3
We still have a variable that is the smallest so far. The first time through the
loop smallest is None so we take the first value to be the smallest.
Summary
While loops (indefinite)
Infinite loops
Using break
Using continue
For loops (definite)
Iteration variables
Largest or smallest