Python Practice 3
Python Practice 3
No Yes Program:
n>0? 5
n = 5
4
print(n) while n > 0 : 3
print(n) 2
n = n -1
n = n – 1 1
print('Blastoff!') Blastoff!
print(n)
0
Loops (repeated steps) have iteration variables that
print('Blastoff') change each time through a loop. Often these iteration
variables go through a sequence of numbers.
n=5 An Infinite Loop
No Yes
n>0?
n = 5
while n > 0 :
print('Lather')
print('Lather')
print('Rinse')
print('Rinse') print('Dry off!')
• It is like a loop test that can happen anywhere in the body of the
loop > hello there
while True:
line = input('> ') hello there
if line == ‘im done' : > finished
break finished
print(line) > im done
print(‘im Done bye!!!!!') im Done bye!!!!!
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 > hello there
while True:
line = input('> ') hello there
if line == ‘im done' : > finished
break finished
print(line) > im done
print(‘im Done bye!!!!!') im Done bye!!!!!
No Yes
while True: True ?
line = input('> ')
if line == 'done' : ....
break
print(line)
print('Done!')
break
...
http://en.wikipedia.org/wiki/Transporter_(Star_Trek)
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!
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
• 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
3
What is the Largest Number?
41
What is the Largest Number?
12
What is the Largest Number?
9
What is the Largest Number?
74
What is the Largest Number?
15
What is the Largest Number?
What is the Largest Number?
3 41 12 9 74 15
What is the Largest Number?
largest_so_far -1
What is the Largest Number?
largest_so_far 3
What is the Largest Number?
41
largest_so_far 41
What is the Largest Number?
12
largest_so_far 41
What is the Largest Number?
largest_so_far 41
What is the Largest Number?
74
largest_so_far 74
What is the Largest Number?
15
74
What is the Largest Number?
3 41 12 9 74 15
74
Finding the Largest Value
$ python largest.py
largest_so_far = -1 Before -1
print('Before', largest_so_far)
for the_num in [9, 41, 12, 3, 74, 15] :
9 9
if the_num > largest_so_far : 41 41
largest_so_far = the_num 41 12
print(largest_so_far, the_num) 41 3
74 74
print('After', largest_so_far) 74 15
After 74
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…
Finding the Average in a Loop
$ python averageloop.py
count = 0 Before 0 0
sum = 0
199
print('Before', count, sum)
for value in [9, 41, 12, 3, 74, 15] :
2 50 41
count = count + 1 3 62 12
sum = sum + value 4 65 3
print(count, sum, value) 5 139 74
print('After', count, sum, sum / count) 6 154 15
After 6 154 25.666
How would we change this to make it find the smallest value in the list?
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
smallest = None that can be used in logical
print('Before') expressions
for value in [3, 41, 12, 9, 74, 15] :
if smallest is None : • Implies “is the same as”
smallest = value
elif value < smallest :
smallest = value • Similar to, but stronger than
print(smallest, value) ==
print('After', smallest)
• is not also is a logical
operator
Acknowledgements / Contributions
These slides are Copyright 2010- Charles R. Severance (www. ...
dr-chuck.com) of the University of Michigan School of
Information and open.umich.edu and made available under a
Creative Commons Attribution 4.0 License. Please maintain this
last slide in all copies of the document to comply with the
attribution requirements of the license. If you make a change,
feel free to add your name and organization to the list of
contributors on this page as you republish the materials.