0% found this document useful (0 votes)
8 views

03 - Python Conditions

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

03 - Python Conditions

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Conditions

Lecture 3
Sheet #2
Due Friday 18 Oct

https://forms.gle/tnFaQKL8Voifkz3e6
x=5
Conditional Steps
Yes
x < 10 ?

No print('Smaller') Program:
Output:
x = 5
Yes if x < 10: Smaller
x > 20 ? print('Smaller') Finis
if x > 20:
No print('Bigger') print('Bigger')

print('Finis')

print('Finis')
Comparison Operators
• Boolean expressions ask a Python Meaning
question and produce a Yes or No < Less than
result which we use to control
program flow <= Less than or Equal to
== Equal to
• Boolean expressions using >= Greater than or Equal to
comparison operators evaluate to
> Greater than
True / False or Yes / No
!= Not equal
• Comparison operators look at
variables but do not change the Remember: “=” is used for assignment.
variables
http://en.wikipedia.org/wiki/George_Boole
Comparison Operators
x = 5
if x == 5 :
print('Equals 5') Equals 5
if x > 4 :
print('Greater than 4') Greater than 4
if x >= 5 :
print('Greater than or Equals 5') Greater than or Equals 5
if x < 6 and x > 3:
print(Between 3 and 6') Between 3 and 6 Exclusive
if x >= 6 or x <= 3:
print(‘Not between 3 and 6')
Not Between 3 and 6
if x != 6 and x !=9 and x != 12: Not equal 6 or 9 or 12
print('Not equal 6')
One-Way Decisions
x = 5 Yes
print('Before 5') Before 5 x == 5 ?
if x == 5 :
print('Is 5') Is 5 No print('Is 5’)
print('Is Still 5')
Is Still 5
print('Third 5')
print('Afterwards 5')
Third 5 print('Still 5')
print('Before 6') Afterwards 5
if x == 6 : Before 6 print('Third 5')
print('Is 6')
print('Is Still 6')
print('Third 6')
print('Afterwards 6') Afterwards 6
• Increase indent indent after an if statement
or for statement (after : )

• Maintain indent to indicate the scope of the Indentation


block (which lines are affected by the if/for)

• Reduce indent back to the level of the if


statement or for statement to indicate the
end of the block

• Blank lines are ignored - they do not affect


indentation

• Comments on a line by themselves are


ignored with regard to indentation
increase / maintain after if or for
decrease to indicate end of block
x = 5
if x > 2 :
print('Bigger than 2')
print('Still bigger')
print('Done with 2')

for i in range(5) :
print(i)
if i > 2 :
print('Bigger than 2')
print('Done with i', i)
print('All Done')
Think About begin/end Blocks
x = 5
if x > 2 :
print('Bigger than 2')
print('Still bigger')
print('Done with 2')

for i in range(5) :
print(i)
if i > 2 :
print('Bigger than 2')
print('Done with i', i)
print('All Done')
Nested x>1
yes

Decisions no print('More than one’)

x = 42
if x > 1 : yes
print('More than one') x < 100
if x < 100 :
no
print('Less than 100') print('Less than 100')
print('All done')

print('All Done')
Two-way Decisions
x=4

• Sometimes we want to
do one thing if a logical no yes
x>2
expression is true and
something else if the
expression is false print('Not bigger') print('Bigger')

• It is like a fork in the


road - we must choose
one or the other path but print('All Done')
not both
Two-way Decisions
x=4
with else:
no yes
x = 4 x>2

if x > 2 :
print('Bigger') print('Not bigger') print('Bigger')
else :
print('Smaller')

print('All done')
print('All Done')
Visualize Blocks x=4

no yes
x = 4 x>2

if x > 2 :
print('Bigger') print('Not bigger') print('Bigger')
else :
print('Smaller')

print('All done')
print('All Done')
Multi-way
yes
x<2 print('small')
no
if x < 2 :
yes
print('small')
elif x < 10 :
x < 10 print('Medium')
print('Medium') no
else :
print('LARGE') print('LARGE')
print('All done')

print('All Done')
x=0
Multi-way
yes
x<2 print('small')
x = 0
no
if x < 2 :
yes
print('small')
elif x < 10 :
x < 10 print('Medium')
print('Medium') no
else :
print('LARGE') print('LARGE')
print('All done')

print('All Done')
x=5
Multi-way
yes
x<2 print('small')
x = 5
no
if x < 2 :
yes
print('small')
elif x < 10 :
x < 10 print('Medium')
print('Medium') no
else :
print('LARGE') print('LARGE')
print('All done')

print('All Done')
x = 20
Multi-way
yes
x<2 print('small')
x = 20
no
if x < 2 :
yes
print('small')
elif x < 10 :
x < 10 print('Medium')
print('Medium') no
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')
Puzzle 1: Multi-way Puzzle
Which will never print
regardless of the value for x?
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')
Puzzle 2: Two-way puzzle
What is this program
trying to do?

Change the program to check numbers divisible by 5


Puzzle 3: Swap two numbers
Read two numbers a, b.
Swap them if a is greater than b,
Then print them in ascending order.

For example: if the inputs are 9 , 5 then the output 5 , 9


Puzzle 3: Swap two numbers
Loops and Iteration
n=5 Repeated Steps
Output:
No Yes Program:
n>0? 5
n = 5 4
print(n) while n > 0 :
3
print(n)
n = n – 1 2
n = n -1 print(‘Goodbye!') 1
print(n) Goodbye!
0
Loops (repeated steps) have iteration variables that
print(Goodbye') 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(‘Start') print(Start')
print(‘End')
print(‘End') print(‘Goodbye!')

What is wrong with this loop?


print(‘Goodbye!') What happens if we start with n = 0?
Breaking Out of a Loop
• The break statement ends the current loop and > hello there
jumps to the statement immediately following
hello there
the loop
> hello again
• It is like a loop test that can happen anywhere hello again
in the body of the loop > get me out
get me out
while True: > hello
line = input('> ')
hello
if line == 'done' :
> done
break
print(line)
Goodbye!
print(‘Goodbye!')
Finishing an Iteration with
continue > hello there
The continue statement ends the current
hello there
iteration and jumps to the top of the loop
> # hello again
and starts the next iteration
> # get me out
while True: > hello
line = input('> ') hello
if line[0] == '#' : > hello again
continue hello again
if line == 'done' :
> done
break
Goodbye!
print(line)
print(‘Goodbye!')
No
True ? Yes
while True: line=input()
line = input('> ')
if line[0] == '#' :
continue
if line == 'done' : continue
break
print(line)
print('Done!') break
print(line)

print(‘Goodbye’)
Summary
• Comparison operators • Nested Decisions
== <= >= > < !=
• Multi-way decisions using elif
• Indentation
• Starting Loops
• One-way Decisions
• Two-way decisions:
if: and else:
Exercise 1

Rewrite your pay computation to give the


employee 1.5 times the hourly rate for hours
worked above 40 hours.

Enter Hours: 45 Normal Overtime


Enter Rate: 10 Rate Rate

Pay: 475.0 475 = 40 * 10 + 5 * 15

Overtime Hours
Exercise 2

Perform the pay computations for 5 employees,


input both hours and rate for each employee then
calculate the pay
Hours: 45,60,25,90,15
Rate: 10,7,15,30,120

Pay: 475.0,..,..,..,..
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.

Initial Development: Charles Severance, University of Michigan


School of Information

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy