Py4Inf 05 Iterations
Py4Inf 05 Iterations
Chapter 5
• It is like a loop test that can happen anywhere in the body of the loop
• It is like a loop test that can happen anywhere in the body of the loop
...
while True:
> hello there
line = raw_input('> ')
hello there
if line[0] == '#' :
> # don't print this
continue
> print this!
if line == 'done' :
print this!
break
> done
print line
Done!
print 'Done!'
Finishing an Iteration with continue
• The continue statement ends the current iteration and jumps to the top
of the loop and starts the next iteration
while True:
> hello there
line = raw_input('> ')
hello there
if line[0] == '#' :
> # don't print this
continue
> print this!
if line == 'done' :
print this!
break
> done
print line
Done!
print 'Done!'
No
True ? Yes
while True:
line = raw_input('> ') ....
if line[0] == '#' :
continue
if line == 'done' : continue
break
print line
print 'Done!' ...
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"
• 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
for i in [5, 4, 3, 2, 1] : 4
print i 3
2
print 'Blastoff!' 1
Blastoff!
A Simple Definite Loop
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
3 41 12 9 74 15
What is the Largest Number?
3 41 12 9 74 15
largest_so_far -13 41 74
Looping through a Set
$ python basicloop.py
Before
3
print 'Before'
41
for thing in [3, 41, 12, 9, 74, 15] :
12
print thing
9
print 'After'
74
15
After
Finding the largest value
largest = -1 $ python largest.py
print 'Before', largest Before -1
for value in [3, 41, 12, 9, 74, 15] : 33
if value > largest : 41 41
largest = value 41 12
print largest, value 41 9
74 74
print 'After', largest 74 15
After 74
We make a variable that contains the largest value we have seen so far. If the
current value is larger, it becomes the new largest value we have seen so far.
Making “smart” loops
Set some variables to initial
values
for thing in data:
• The trick is “knowing” something
Look for something or
about the whole loop when you
do something to each
are stuck writing code that only
entry separately,
sees one entry at a time updating a variable.
We make a variable that contains the largest value we have seen so far. If the
current value is larger, it becomes the new largest value we have seen so far.
Finding the smallest value?
smallest = -1
print 'Before', smallest
for value in [3, 41, 12, 9, 74, 15] :
if value < smallest :
smallest = value
print smallest, value
We make a variable that contains the smallest value we have seen so far. If the
current value is smaller, it becomes the new smallest value we have seen so far.
What is the Smallest Number?
3 41 12 9 74 15
smallest -1
Finding the smallest value?
smallest = -1
print 'Before', largest
for value in [3, 41, 12, 9, 74, 15] :
if value < smallest :
smallest = value
print smallest, value
We make a variable that contains the smallest value we have seen so far. If the
current value is smaller, it becomes the new smallest value we have seen so far.
Finding the smallest value
smallest = None
print 'Before' $ python smallest.py
for value in [9, 41, 12, 3, 74, 15] : Before
if smallest is None : 99
smallest = value 9 41
elif value < smallest : 9 12
smallest = value 33
print smallest, value 3 74
3 15
print 'After', smallest 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.
The "is" and "is not" Operators
smallest = None
print 'Before' • Python has an "is" operaror that
can be used in logical
for value in [9, 41, 12, 3, 74, 15] :
expressions
if smallest is None :
smallest = value
elif value < smallest : • Implies 'is the same as'
smallest = value
print smallest, value
• Similar to, but stronger than ==