Docsity

Python Programming conditional Execution, Lecture notes of Computer Science

Partial preview of the text

Download Python Programming conditional Execution and more Lecture notes Computer Science in PDF only on Docsity!

Python Programming

Conditional Execution

Python for Everybody www.py4e.com

Conditional Steps

Output:

Smaller

Finis

Program:

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

Comparison Operators

x = 5 if x == 5 : print('Equals 5') if x > 4 : print('Greater than 4 ') if x >= 5 : print('Greater than or Equals 5') if x < 6 : print('Less than 6') if x <= 5 : print('Less than or Equals 5') if x != 6 : print('Not equal 6')

Equals 5

Greater than 4

Greater than or Equals 5

Less than 6

Less than or Equals 5

Not equal 6

One-Way Decisions x = 5 print('Before 5 ') if x == 5 : print('Is 5 ') print('Is Still 5 ') print('Third 5 ') print('Afterwards 5') print('Before 6') if x == 6 : print('Is 6') print('Is Still 6') print('Third 6') print('Afterwards 6')

Before 5

Is 5

Is Still 5

Third 5

Afterwards 5

Before 6

Afterwards 6

x == 5?

Yes

print('Still 5') print('Third 5')

No print('Is 5’)

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')

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

Two-way Decisions

  • Sometimes we want to

do one thing if a logical

expression is true and

something else if the

expression is false

  • It is like a fork in the

road - we must choose

one or the other path but

not both

x > 2 print('Bigger') no yes x = 4 print('Not bigger')

print('All Done')

Two-way Decisions with else: x > 2 print('Bigger') no yes x = 4

print('All Done')

x = 4 if x > 2 : print('Bigger') else : print('Smaller') print('All done') print('Not bigger')

More Conditional Structures…

Multi-way

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

print('All Done')

x < 10 print('Medium') yes print('LARGE') no

Multi-way

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

print('All Done')

x < 10 print('Medium') yes print('LARGE') no

x = 5

Multi-way

x = 20 if x < 2 : print('small') elif x < 10 : print('Medium') else : print('LARGE') print('All done' ) x < 2 print('small') yes no

print('All Done')

x < 10 print('Medium') yes print('LARGE') no

x = 20

Multi-way Puzzles

if x < 2 : print('Below 2') elif x < 20 : print('Below 20') elif x < 10 : print('Below 10') else : print('Something else') if x < 2 : print('Below 2') elif x >= 2 : print('Two or more') else : print('Something else' )

Which will never print

regardless of the value for x?

The try / except Structure

• You surround a dangerous section of code with try and except

• If the code in the try works - the except is skipped

• If the code in the try fails - it jumps to the except section