Lecture 5
Lecture 5
Application Programming
with Python
Control structure
in python
1
4/17/2023
2
4/17/2023
3
4/17/2023
4
4/17/2023
False
Condition ? Statement 1 Statement 2
True
Statement 1
else
Statement 2 body
Main
Body
10
10
5
4/17/2023
11
12
6
4/17/2023
13
13
while loop
14
7
4/17/2023
15
16
8
4/17/2023
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!
17
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!
18
9
4/17/2023
...
print('Done')
19
while True:
> hello there
line = 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!')
20
10
4/17/2023
while True:
line = input('> ') > hello there
if line[0] == '#' : hello there
> # don't print this
continue
> print this!
if line == 'done' : print this!
break > done
print(line) Done!
print('Done!')
21
No
True ? Yes
while True:
line = input('> ')
....
if line[0] == '#' :
continue
if line == 'done' : continue
break
print(line) ...
print('Done!')
print('Done')
22
11
4/17/2023
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”
23
Definite Loops
Iterating over a set of items…
24
12
4/17/2023
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
25
26
13
4/17/2023
27
28
14
4/17/2023
Looking at in...
The iteration variable
“iterates” through the Five-element
sequence (ordered set) sequence
Iteration variable
The block (body) of code is
executed once for each value for i in [5, 4, 3, 2, 1] :
in the sequence print(i)
29
30
15
4/17/2023
i=5
No print(i)
Yes
Done? Move i ahead i=4
print(i)
print(i)
i=3
print(i)
i=2
for i in [5, 4, 3, 2, 1] : print(i)
print(i) i=1
print(i)
31
Loop Idioms:
What We Do in Loops
32
16
4/17/2023
33
34
17
4/17/2023
35
36
18
4/17/2023
41
37
12
38
19
4/17/2023
39
74
40
20
4/17/2023
15
41
42
21
4/17/2023
3 41 12 9 74 15
43
largest_so_far -1
44
22
4/17/2023
largest_so_far 3
45
41
largest_so_far 41
46
23
4/17/2023
12
largest_so_far 41
47
largest_so_far 41
48
24
4/17/2023
74
largest_so_far 74
49
15
74
50
25
4/17/2023
3 41 12 9 74 15
74
51
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.
52
26
4/17/2023
53
Counting in a Loop
zork = 0
print('Before', zork)
$ python
for thing in [9, 41, 12, 3, 74, 15] : countloop.py
zork = zork + 1 Before 0
print(zork, thing)
print('After', zork)
19
2 41
To count how many times we execute a loop, we 3 12
introduce a counter variable that starts at 0 and we add 43
one to it each time through the loop. 5 74
6 15
After 6
54
27
4/17/2023
Summing in a Loop
zork = 0 $ python
print('Before', zork) countloop.py
for thing in [9, 41, 12, 3, 74, 15]: Before 0
zork = zork + thing 99
print(zork, thing) 50 41
print('After', zork) 62 12
65 3
To add up a value we encounter in a loop, we introduce 139 74
a sum variable that starts at 0 and we add the value to the 154 15
sum each time through the loop. After 154
55
56
28
4/17/2023
Filtering in a Loop
print('Before') $ python search1.py
for value in [9, 41, 12, 3, 74, 15] : Before
if value > 20: Large number 41
print('Large number',value) Large number 74
After
print('After')
57
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.
58
29
4/17/2023
How would we change this to make it find the smallest value in the list?
59
print('After', smallest_so_far)
We switched the variable name to smallest_so_far and switched the > to <
60
30
4/17/2023
We switched the variable name to smallest_so_far and switched the > to <
61
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.
62
31
4/17/2023
63
Summary
• While loops (indefinite) • For loops (definite)
• Infinite loops • Iteration variables
• Using break • Loop idioms
• Using continue • Largest or smallest
• None constants and variables
64
32
4/17/2023
Function Description
int(x [,base]) Data Type Conversion:
Converts x to an integer. base specifies the base if x is a string.
long(x [,base] ) Converts x to a long integer. base specifies the base if x is a string.
float(x) Converts x to a floating-point number.
65
Acknowledgements / Contributions
66
33