4 Python4-Iterations
4 Python4-Iterations
Chapter 5
Outline
• While loops (indefinite) • For loops (definite)
• Infinite loops • Iteration variables
• Using break • Loop idioms
• Using continue • Largest or smallest
8-3
Copyright © 2016 Pearson Education, Inc., Hoboken NJ
While Loops
• while loop is used to execute a block of statements repeatedly until
a given a condition is satisfied.
• When the condition becomes false, the line immediately after the
loop in program is executed
• It is like a loop test that can happen anywhere in the body of the
loop
while True: > hello there
line = input('> ') hello there
if line == 'done' : > finished
break finished
print(line) > done
print('Done!') Done!
Breaking Out of a Loop
• The break statement ends the current loop and jumps to the
statement immediately following the loop
• It is like a loop test that can happen anywhere in the body of the
loop
while True: > hello there
line = input('> ') hello there
if line == 'done' : > finished
break finished
print(line) > done
print('Done!') Done!
No Yes
while True: True ?
line = input('> ')
if line == 'done' :
....
break
print(line)
print('Done!')
break
...
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 = input('> ')
if line[0] == '#' : hello there
continue > # don't print this
if line == 'done' : > print this!
break print this!
print(line) > done
print('Done!') 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 = input('> ')
if line[0] == '#' : hello there
continue > # don't print this
if line == 'done' : > print this!
break print this!
print(line) > done
print('Done!') Done!
No
True ? Yes
while True:
line = raw_input('> ') ....
if line[0] == '#' :
continue
if line == 'done' : continue
break
print(line)
...
print('Done!')
print('Done')
Write a Python program that ask the user to enter 5 numbers from 0
to 9. If the user enters 0 then end the program, if the user enters 5
or 7 then do nothing, otherwise print the number.
Indefinite Loops
• 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
i=2
We make a variable that contains the largest value we have seen so far. If the current
number we are looking at is larger, it is the new largest value we have seen so far.
More Loop Patterns…
Counting in a Loop
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.
Search Using a Boolean Variable
$ python search1.py
found = False
Before False
print('Before', found) False 9
for value in [9, 41, 12, 3, 74, 15] : False 41
if value == 3 : False 12
found = True True 3 Is it ok if I stop looping
here?
print(found, value) True 74 How?
print('After', found)
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.
How to Find the Smallest Value
How would we change this to make it find the smallest value in the list?
$ python largest.py
largest_so_far = -1 Before -1
print('Before', largest_so_far) 9 9
for the_num in [9, 41, 12, 3, 74, 15] : 41 41
if the_num > largest_so_far : 41 12
largest_so_far = the_num
print(largest_so_far, the_num)
41 3
74 74
print('After', largest_so_far) 74 15
After 74
Finding the Smallest Value
smallest_so_far = -1
print('Before', smallest_so_far)
for the_num in [9, 41, 12, 3, 74, 15] :
if the_num < smallest_so_far :
smallest_so_far = the_num
print(smallest_so_far, the_num)
print('After', smallest_so_far)
We switched the variable name to smallest_so_far and switched the > to <
Finding the Smallest Value
$ python smallbad.py
smallest_so_far = -1
Before -1
print('Before', smallest_so_far)
for the_num in [9, 41, 12, 3, 74, 15] :
-1 9
if the_num < smallest_so_far : -1 41
smallest_so_far = the_num -1 12
print(smallest_so_far, the_num) -1 3
-1 74
print('After', smallest_so_far) -1 15
After -1
We switched the variable name to smallest_so_far and switched the > to <
Finding the Smallest Value
smallest = None $ python smallest.py
print('Before') Before
for value in [9, 41, 12, 3, 74, 15] : 99
if smallest is None :
9 41
smallest = value
elif value < smallest : 9 12
smallest = value 33
print(smallest, value) 3 74
print('After', smallest) 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.
The is and is not Operators
• Python has an is operator
that can be used in logical
expressions
smallest = None
print('Before') • Implies “is the same as”,
for value in [3, 41, 12, 9, 74, 15] : None
if smallest is None : boolean
smallest = value
elif value < smallest :
smallest = value • Similar to, but stronger than
print(smallest, value) ==
0==0.0 True
print('After', smallest)
0 is 0.0 False
• Nested loops