Python - Control Structures
Python - Control Structures
• 3 control structures
– Sequential structure
• Built into Python
– Selection structure
• The if statement
• The if/else statement
• The if/elif/else statement
– Repetition structure
• The while repetition structure
• The for repetition structure
if Selection Structure
true
Grade >= 60 print “Passed”
false
if/else Structure
false true
Grade >= 60
true
if statement condition a case a action(s)
false
true
first elif condition b case b action(s)
statement false
.
.
.
if price < 1:
s = “That’s cheap, buy a lot!”
else:
s = “Too much, buy some carrots instead”
print s
Expression values?
Python 2.2b2 (#26, Nov 16 2001, 11:44:11) [MSC 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> if 0:
... print "0 is true"
... else:
... print "0 is false"
...
0 is false
>>> if 1:
... print "non-zero is true"
...
non-zero is true
>>> if -1:
... print "non-zero is true"
...
non-zero is true
>>> print 2 < 3
1
• Operators
– and
• Binary. Evaluates to true if both expressions are true
– or
• Binary. Evaluates to true if at least one expression is true
– not
• Unary. Returns true if the expression is false
if not found_what_we_need:
print “didn’t find item”
Keywords
Python
keywords
and continue else for import not raise
assert def except from in or return
break del exec global is pass try
class elif finally if lambda print while
• Repetition Structures
– Allow a program to repeat an action while a condition is true
• Using while Repetition
– Action(s) contained within the body of the loop
– Condition should evaluate to false at some point
• Otherwise infinite loop and program hangs
true
Product <= 1000 Product = 2 * product
false
Example Program
num = 1
# loop will repeat itself as long as
# num < 10 remains true
while num < 10:
print(num)
#incrementing the value of num
num = num + 3
Output
1
4
7
2002 Prentice Hall. All rights reserved.
19
1 # Fig. 3.10: fig03_10.py
2 # Class average program with counter-controlled repetition.
3 The total and counter, set to
4 # initialization phase
5 total = 0 # sum of grades zero and one respectively Fig03_10.py
6 gradeCounter = 1 # number of grades entered A loop the continues as long as
7
8 # processing phase the counter does not go past 10
9 while gradeCounter <= 10: # loop 10 times
10 grade = raw_input( "Enter grade: " ) # get one grade
11 grade = int( grade ) # convert string to an integer
12 total = total + grade Adds one to the counter to
13 gradeCounter = gradeCounter + 1
14
eventually break the loop
15 # termination phase
16 average = total / 10 # integer division
Divides the total by the 10
17 print "Class average is", average to get the class average
Enter grade: 98
Enter grade: 76 Program Output
Enter grade: 71
Enter grade: 87
Enter grade: 83
Enter grade: 90
Enter grade: 57
Enter grade: 79
Enter grade: 82
Enter grade: 94
Class average is 81 2002 Prentice Hall.
All rights reserved.
3.9 Formulating Algorithms: Case 20
Study 2 (Sentinel-Controlled
Repetition)
• Sentinel (guard) Value
– A dummy value, one that the program checks for in order to
break out of the loop
– Sentinel values can be entered in by the user
– Known as indefinite repetition
• The total number of loops is unknown
Study 2 (Sentinel-Controlled
Repetition)
Initialize total to zero
Initialize counter to zero
0
1 Program Output
2
3
4
5
6
7
8
9
range function
Python 2.2b2 (#26, Nov 16 2001, 11:44:11) [MSC 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> range( 10 )
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Python 2.2b2 (#26, Nov 16 2001, 11:44:11) [MSC 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> range( 10, 0, -1 )
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
Another example
1 2 3 4 Program Output
Broke out of loop at x = 5
1 2 3 4 6 7 8 9 10
Used continue to skip printing the value 5 Program Output