Pythonlearn 03 Conditional
Pythonlearn 03 Conditional
x=5
Conditional Steps
Yes
x < 10 ?
Program:
Conditional Execution No print('Smaller') Output:
x = 5
Smaller
Chapter 3 x > 20 ?
Yes if x < 10:
print('Smaller') Finis
if x > 20:
No print('Bigger')
print('Bigger')
print('Finis')
Python for Everybody
www.py4e.com print('Finis')
1 2
3 4
10/01/21
One-Way Decisions
x = 5 Yes Indentation
print('Before 5') Before 5 x == 5 ?
if x == 5 :
• Increase indent indent after an if statement or for statement (after : )
print('Is 5') Is 5 No print('Is 5’)
print('Is Still 5')
Is Still 5 • Maintain indent to indicate the scope of the block (which lines are affected
print('Third 5')
Third 5 print('Still 5') by the if/for)
print('Afterwards 5')
print('Before 6') Afterwards 5
• Reduce indent back to the level of the if statement or for statement to
if x == 6 : Before 6 print('Third 5') indicate the end of the block
print('Is 6')
print('Is Still 6') • Blank lines are ignored - they do not affect indentation
print('Third 6')
print('Afterwards 6') Afterwards 6 • Comments on a line by themselves are ignored with regard to indentation
5 6
• Most text editors can turn tabs into spaces - make sure to enable this
feature
• Python cares a *lot* about how far a line is indented. If you mix tabs and
spaces, you may get “indentation errors” even if everything looks fine
7 8
10/01/21
9 10
Nested x>1
yes
Two-way Decisions
Decisions no
x=4
print('More than one’) • Sometimes we want to
x = 42 do one thing if a logical no yes
x>2
if x > 1 : yes expression is true and
x < 100
print('More than one') something else if the
if x < 100 : expression is false print('Not bigger') print('Bigger')
no
print('Less than 100') print('Less than 100')
print('All done')
• It is like a fork in the
road - we must choose
one or the other path but print('All Done')
print('All Done')
not both
11 12
10/01/21
if x > 2 : if x > 2 :
print('Bigger') print('Not bigger') print('Bigger') print('Bigger') print('Not bigger') print('Bigger')
else : else :
print('Smaller') print('Smaller')
13 14
Multi-way
yes
x<2 print('small')
print('All Done')
15 16
10/01/21
x=0 x=5
Multi-way Multi-way
yes yes
x<2 print('small') x<2 print('small')
x = 0 x = 5
no no
if x < 2 : if x < 2 :
yes yes
print('small') print('small')
elif x < 10 :
x < 10 print('Medium')
elif x < 10 :
x < 10 print('Medium')
print('Medium') no print('Medium') no
else : else :
print('LARGE') print('LARGE') print('LARGE') print('LARGE')
print('All done') print('All done')
17 18
x = 20
Multi-way Multi-way if x < 2 :
yes print('Small')
x<2 print('small') elif x < 10 :
x = 20 # No Else print('Medium')
no elif x < 20 :
if x < 2 : x = 5
yes print('Big')
print('small') if x < 2 :
x < 10 print('Medium') elif x < 40 :
elif x < 10 : print('Small')
print('Medium') no elif x < 10 : print('Large')
else : print('Medium') elif x < 100:
print('LARGE') print('LARGE') print('Huge')
print('All done') print('All done') else :
print('Ginormous')
print('All Done')
19 20
10/01/21
21 22
23 24
10/01/21
Generic Generic
Software Software
Computer Computer
Input Input
Central Central
Devices Devices
Processing Processing
Unit Unit
Secondary Secondary
Memory Memory
25 26
istr = int(astr)
except: print('Hello')
istr = -1 astr = 'Bob'
$ python tryexcept.py try:
print('First', istr) First -1 print('Hello') istr = int(astr)
Second 123 istr = int(astr)
astr = '123' print('There')
try: except: print('There')
istr = int(astr) istr = -1
except:
istr = -1
istr = -1 When the second conversion print('Done', istr)
succeeds - it just skips the except: Safety net
print('Second', istr) clause and the program continues. print('Done', istr)
27 28
10/01/21
29 30
Exercise Exercise
31 32
10/01/21
Acknowledgements / Contributions
These slides are Copyright 2010- Charles R. Severance
(www.dr-chuck.com) of the University of Michigan School of ...
Information 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.
33