Py4Inf 03 Conditional
Py4Inf 03 Conditional
Chapter 3
print 'Finis'
print 'Finis'
Comparison Operators
• Boolean expressions ask a
Python Meaning
question and produce a Yes or
No result which we use to < Less than
control program flow <= Less than or Equal
== Equal to
• Boolean expressions using >= Greater than or Equal
comparison operators evaluate
> Greater than
to - True / False - Yes / No
!= Not equal
• Comparison operators look at
variables but do not change the
variables Remember: “=” is used for assignment.
http://en.wikipedia.org/wiki/George_Boole
x=5
if x == 5 : Comparison
print 'Equals 5'
Operators
if x > 4 :
print 'Greater than 4'
if x >= 5 : Equals 5
print 'Greater than or Equal 5' Greater than 4
Greater than or Equal 5
if x < 6 : print 'Less than 6' Less than 6
Less than or Equal 5
if x <= 5 : Not equal 6
print 'Less than or Equal 5'
if x != 6 :
print 'Not equal 6'
Boolean Operations
• Maintain indent to indicate the scope of the block (which lines are
affected by the if/for)
• Python cares a *lot* about how far line is indented. If you mix tabs
and spaces, you may get “indentation errors” even if everything looks
fine
Please do this now while you are thinking about it so we can all stay sane...
This will save you
much unnecessary
pain.
increase / maintain after if or for
decrease to indicate end of block
blank lines and comment lines ignored
x=5
x=5
if x > 2 :
if x > 2 :
# comments
print 'Bigger than 2'
print 'Still bigger'
print 'Bigger than 2'
print 'Done with 2'
# don’t matter
print 'Still bigger'
for i in range(5) :
# but can confuse you
print i
if i > 2 :
print 'Done with 2'
print 'Bigger than 2'
# if you don’t line
print 'Done with i', i
# them up
Mental begin/end squares
x=5 x=5
if x > 2 : if x > 2 :
print 'Bigger than 2' # comments
print 'Still bigger'
print 'Done with 2' print 'Bigger than 2'
# don’t matter
for i in range(5) : print 'Still bigger'
print i # but can confuse you
if i > 2 :
print 'Bigger than 2' print 'Done with 2'
print 'Done with i', i # if you don’t line
# them up
yes
Nested x>1
x = 42
yes
if x > 1 : x < 100
x = 42
yes
if x > 1 : x < 100
x = 42
yes
if x > 1 : x < 100
• Sometimes we want to no
x>2
yes
do one thing if a logical
expression is true and
something else if the
print 'Not bigger' print 'Bigger'
expression is false
no yes
x=4 x>2
if x > 2 :
print 'Smaller' print 'Bigger'
print 'Bigger'
else :
print 'Smaller'
print 'All Done'
print 'All done'
Two-way
using else :
X=4
no yes
x=4 x>2
if x > 2 :
print 'Smaller' print 'Bigger'
print 'Bigger'
else :
print 'Smaller'
print 'All Done'
print 'All done'
Multi-way
yes
x<2 print 'Small'
if x < 2 : no
print 'Small'
yes
elif x < 10 :
x<10 print 'Medium'
print 'Medium' no
else :
print 'LARGE' print 'LARGE'
print 'All done'
yes
x<2 print 'Small'
x=0 no
if x < 2 : yes
print 'Small' x<10 print 'Medium'
elif x < 10 : no
print 'Medium'
else : print 'LARGE'
print 'LARGE'
print 'All done'
print 'All Done'
Multi-way X=5
yes
x<2 print 'Small'
x=5 no
if x < 2 : yes
print 'Small' x<10 print 'Medium'
elif x < 10 : no
print 'Medium'
else : print 'LARGE'
print 'LARGE'
print 'All done'
print 'All Done'
Multi-way X = 20
yes
x<2 print 'Small'
x = 20 no
if x < 2 : yes
print 'Small' x<10 print 'Medium'
elif x < 10 : no
print 'Medium'
else : print 'LARGE'
print 'LARGE'
print 'All done'
print 'All Done'
Multi-way if x < 2 :
print 'Small'
elif x < 10 :
# No Else print 'Medium'
x=5 elif x < 20 :
if x < 2 : print 'Big'
print 'Small' elif x< 40 :
elif x < 10 : print 'Large'
print 'Medium' elif x < 100:
print 'Huge'
print 'All done' else :
print 'Ginormous'
Multi-way Puzzles
Which will never print? if x < 2 :
print 'Below 2'
if x < 2 : elif x < 20 :
print 'Below 2' print 'Below 20'
elif x >= 2 : elif x < 10 :
print 'Two or more' print 'Below 10'
else : else :
print 'Something else' print 'Something else'
The try / except Structure
Output Main
Devices Memory
$ cat tryexcept.py
astr = 'Hello Bob' When the first conversion fails - it
try: just drops into the except: clause and
istr = int(astr) the program continues.
except:
istr = -1
$ python tryexcept.py
print 'First', istr
First -1
Second 123
astr = '123'
try:
istr = int(astr) When the second conversion
except: succeeds - it just skips the except:
istr = -1 clause and the program continues.
print 'Hello'
astr = 'Bob'
try:
print 'Hello' istr = int(astr)
istr = int(astr)
print 'There' print 'There'
except:
istr = -1 istr = -1
try:
$ python trynum.py
ival = int(rawstr)
Enter a number:42
except:
Nice work
ival = -1
$ python trynum.py
Enter a number:fourtytwo
if ival > 0 :
Not a number
print 'Nice work'
$
else:
print 'Not a number'
Exercise
Enter Hours: 45
Enter Rate: 10
Pay: 475.0
475 = 40 * 10 + 5 * 15
Exercise
Rewrite your pay program using try and except so
that your program handles non-numeric input
gracefully.
Enter Hours: 20
Enter Rate: nine
Error, please enter numeric input