[301] Conditions Based on slides created by Tyler Caraza-Harter - - PowerPoint PPT Presentation

301 conditions
SMART_READER_LITE
LIVE PREVIEW

[301] Conditions Based on slides created by Tyler Caraza-Harter - - PowerPoint PPT Presentation

[301] Conditions Based on slides created by Tyler Caraza-Harter Learning Objectives Today Reason about conditions Conditional execution Chapter 5 of Think Python Alternate execution (skip Recursion sections) Chained execution


slide-1
SLIDE 1

[301] Conditions

Based on slides created by Tyler Caraza-Harter

slide-2
SLIDE 2

Learning Objectives Today

Reason about conditions

  • Conditional execution
  • Alternate execution
  • Chained execution
  • Nested conditions

Understand code blocks

  • Be able to identify the lines of code in the same block

Sanity checking

  • Recognize errors
  • Sanitize bad data automatically

Chapter 5 of Think Python (skip “Recursion” sections)

slide-3
SLIDE 3

Today's Outline

Review Control Flow Diagrams Basic syntax for “if” Identifying code blocks Demos

slide-4
SLIDE 4

Indentation Example

print(“A”)
 print(“B”)
 
 def print_letters():
 print(“C”)
 print(“D”) print(“E”)
 print(“F”) print_letters()

what does it print?

slide-5
SLIDE 5

Indentation Example

print(“A”)
 print(“B”)
 
 def print_letters():
 print(“C”)
 print(“D”) print(“E”)
 print(“F”) print_letters()

what does it print?

A
 B
 E
 F
 C
 D

slide-6
SLIDE 6

Indentation Example

print(“A”)
 print(“B”)
 
 def print_letters():
 print(“C”)
 print(“D”) print(“E”)
 print(“F”) print_letters()

what does it print?

A
 B
 E
 F
 C
 D

indented, so “inside”
 print_letters function

slide-7
SLIDE 7

Indentation Example

print(“A”)
 print(“B”)
 
 def print_letters():
 print(“C”)
 print(“D”) print(“E”)
 print(“F”) print_letters()

what does it print?

A
 B
 E
 F
 C
 D

indented, so “inside”
 print_letters function printed last because
 print_letters is called last

slide-8
SLIDE 8

Indentation Example

print(“A”)
 print(“B”)
 
 def print_letters():
 print(“C”)
 print(“D”) print(“E”)
 print(“F”) print_letters()

what does it print?

A
 B
 E
 F
 C
 D

indented, so “inside”
 print_letters function

slide-9
SLIDE 9

Indentation Example

print(“A”)
 print(“B”)
 
 def print_letters():
 print(“C”)
 print(“D”) print(“E”)
 print(“F”) print_letters()

what does it print?

A
 B
 E
 F
 C
 D

indented, so “inside”
 print_letters function not indented, so “outside” any function

slide-10
SLIDE 10

Indentation Example

print(“A”)
 print(“B”)
 
 def print_letters():
 print(“C”)
 print(“D”) print(“E”)
 print(“F”) print_letters()

what does it print?

A
 B
 E
 F
 C
 D

indented, so “inside”
 print_letters function not indented, so “outside” any function also not indented, so “outside” any function. Runs BEFORE
 print_letters is called

slide-11
SLIDE 11

Indentation Example

print(“A”)
 print(“B”)
 
 def print_letters():
 print(“C”)
 print(“D”) print(“E”)
 print(“F”) print_letters()

what does it print?

A
 B
 E
 F
 C
 D

indented, so “inside”
 print_letters function not indented, so “outside” any function also not indented, so “outside” any function. Runs BEFORE
 print_letters is called We use indenting to tell Python which code is inside or outside


  • f a function (or other things we’ll learn about soon).
slide-12
SLIDE 12

Indentation Example

print(“A”)
 print(“B”)
 
 def print_letters():
 print(“C”)
 print(“D”) print(“E”)
 print(“F”) print_letters()

what does it print?

A
 B
 E
 F
 C
 D

indented, so “inside”
 print_letters function not indented, so “outside” any function also not indented, so “outside” any function. Runs BEFORE
 print_letters is called We use indenting to tell Python which code is inside or outside


  • f a function (or other things we’ll learn about soon).

blank lines are irrelevant

slide-13
SLIDE 13

Indentation Example

print(“A”)
 print(“B”)
 
 def print_letters():
 print(“C”)
 print(“D”) print(“E”)
 print(“F”) print_letters()

what does it print?

A
 B
 E
 F
 C
 D

we’ll often call the lines


  • f code inside something


a “block” of code

slide-14
SLIDE 14

Today's Outline

Review Control Flow Diagrams Basic syntax for “if” Identifying code blocks Demos

slide-15
SLIDE 15

Control Flow Diagrams

x = input(“enter x: ”) x = int(x) x % 2 == 0 print(“it’s odd”) print(“it’s even”) print(“thank you”)

True False

slide-16
SLIDE 16

Control Flow Diagrams

x = input(“enter x: ”) x = int(x) x % 2 == 0 print(“it’s odd”) print(“it’s even”) print(“thank you”)

True False Sometimes
 we do this

slide-17
SLIDE 17

Control Flow Diagrams

x = input(“enter x: ”) x = int(x) x % 2 == 0 print(“it’s odd”) print(“it’s even”) print(“thank you”)

True False Sometimes
 we do this Other times
 we do this

slide-18
SLIDE 18

Control Flow Diagrams

x = input(“enter x: ”) x = int(x) x % 2 == 0 print(“it’s odd”) print(“it’s even”) print(“thank you”)

True False Sometimes
 we do this Other times
 we do this

condition

slide-19
SLIDE 19

Control Flow Diagrams

x = input(“enter x: ”) x = int(x) x % 2 == 0 print(“it’s odd”) print(“it’s even”) print(“thank you”)

True False Sometimes
 we do this Other times
 we do this

boolean expressions are mostly
 used for deciding what to do next
 (not for printing “True” or “False”
 is in most of our examples thus far) condition

slide-20
SLIDE 20

“Paths of Execution”

x = input(“enter x: ”) x = int(x) x % 2 == 0 print(“it’s odd”) print(“it’s even”) print(“thank you”)

True False enter x: 7 it’s odd thank you Input/Output:

slide-21
SLIDE 21

“Paths of Execution”

x = input(“enter x: ”) x = int(x) x % 2 == 0 print(“it’s odd”) print(“it’s even”) print(“thank you”)

True False enter x: 8 it’s even thank you Input/Output:

slide-22
SLIDE 22

Today's Outline

Review Control Flow Diagrams Basic syntax for “if” Identifying code blocks Demos

slide-23
SLIDE 23

Writing conditions in Python

x = input(“enter x: ”) x = int(x) x % 2 == 0 print(“it’s odd”) print(“it’s even”) print(“thank you”) True False

Code:

slide-24
SLIDE 24

Writing conditions in Python

x = input(“enter x: ”) x = int(x) x % 2 == 0 print(“it’s odd”) print(“it’s even”) print(“thank you”) True False

Code: x = input(“enter x: ”) x = int(x)

slide-25
SLIDE 25

Writing conditions in Python

x = input(“enter x: ”) x = int(x) x % 2 == 0 print(“it’s odd”) print(“it’s even”) print(“thank you”) True False

Code: x = input(“enter x: ”) x = int(x) if x % 2 == 0:

slide-26
SLIDE 26

Writing conditions in Python

x = input(“enter x: ”) x = int(x) x % 2 == 0 print(“it’s odd”) print(“it’s even”) print(“thank you”) True False

Code: x = input(“enter x: ”) x = int(x) if x % 2 == 0: boolean expression

slide-27
SLIDE 27

Writing conditions in Python

x = input(“enter x: ”) x = int(x) x % 2 == 0 print(“it’s odd”) print(“it’s even”) print(“thank you”) True False

Code: x = input(“enter x: ”) x = int(x) if x % 2 == 0: print(“it’s even”)

slide-28
SLIDE 28

Writing conditions in Python

x = input(“enter x: ”) x = int(x) x % 2 == 0 print(“it’s odd”) print(“it’s even”) print(“thank you”) True False

Code: x = input(“enter x: ”) x = int(x) if x % 2 == 0: print(“it’s even”) else: print(“it’s odd”)
 


slide-29
SLIDE 29

Writing conditions in Python

x = input(“enter x: ”) x = int(x) x % 2 == 0 print(“it’s odd”) print(“it’s even”) print(“thank you”) True False

Code: x = input(“enter x: ”) x = int(x) if x % 2 == 0: print(“it’s even”) else: print(“it’s odd”)
 
 print(“thank you”)

slide-30
SLIDE 30

Writing conditions in Python

x = input(“enter x: ”) x = int(x) x % 2 == 0 print(“it’s odd”) print(“it’s even”) print(“thank you”) True False

Code: x = input(“enter x: ”) x = int(x) if x % 2 == 0: print(“it’s even”) else: print(“it’s odd”)
 
 print(“thank you”)

slide-31
SLIDE 31

Writing conditions in Python

x = input(“enter x: ”) x = int(x) x % 2 == 0 print(“it’s odd”) print(“good!”) print(“it’s even”) print(“thank you”) True False

Code: x = input(“enter x: ”) x = int(x) if x % 2 == 0: print(“it’s even”) else: print(“it’s odd”) print(“good!”)
 
 print(“thank you”)

slide-32
SLIDE 32

Writing conditions in Python

x = input(“enter x: ”) x = int(x) x % 2 == 0 print(“it’s odd”) print(“good!”) print(“it’s even”)
 print(“we wanted odd”) print(“thank you”) True False

Code: x = input(“enter x: ”) x = int(x) if x % 2 == 0: print(“it’s even”)
 print(“we wanted odd”) else: print(“it’s odd”) print(“good!”)
 
 print(“thank you”)

slide-33
SLIDE 33

Writing conditions in Python

x = input(“enter x: ”) x = int(x) x % 2 == 0 print(“it’s odd”) print(“good!”) print(“it’s even”)
 print(“we wanted odd”) print(“thank you”) print(“all done”) True False

Code: x = input(“enter x: ”) x = int(x) if x % 2 == 0: print(“it’s even”)
 print(“we wanted odd”) else: print(“it’s odd”) print(“good!”)
 
 print(“thank you”) print(“all done”)

slide-34
SLIDE 34

Today's Outline

Review Control Flow Diagrams Basic syntax for “if” Identifying code blocks Demos

slide-35
SLIDE 35

Code Blocks

Code: x = input(“enter x: ”) x = int(x) if x % 2 == 0: print(“it’s even”)
 print(“we wanted odd”) else: print(“it’s odd”) print(“good!”)
 
 print(“thank you”) print(“all done”)

slide-36
SLIDE 36

Code Blocks

Code: x = input(“enter x: ”) x = int(x) if x % 2 == 0: print(“it’s even”)
 print(“we wanted odd”) else: print(“it’s odd”) print(“good!”)
 
 print(“thank you”) print(“all done”) block of code inside “if”

slide-37
SLIDE 37

Code Blocks

Code: x = input(“enter x: ”) x = int(x) if x % 2 == 0: print(“it’s even”)
 print(“we wanted odd”) else: print(“it’s odd”) print(“good!”)
 
 print(“thank you”) print(“all done”) block of code inside “if” block of code inside “else”

slide-38
SLIDE 38

Code Blocks

Code: x = input(“enter x: ”) x = int(x) if x % 2 == 0: print(“it’s even”)
 print(“we wanted odd”) else: print(“it’s odd”) print(“good!”)
 
 print(“thank you”) print(“all done”) block of code inside “if” block of code inside “else” What if all this were inside a function?

slide-39
SLIDE 39

Code Blocks

Code: def check_oddness():
 x = input(“enter x: ”) x = int(x) if x % 2 == 0: print(“it’s even”)
 print(“we wanted odd”) else: print(“it’s odd”) print(“good!”)
 
 print(“thank you”) print(“all done”) check_oddness() block of code inside “if” block of code inside “else”

slide-40
SLIDE 40

Code Blocks

Code: def check_oddness():
 x = input(“enter x: ”) x = int(x) if x % 2 == 0: print(“it’s even”)
 print(“we wanted odd”) else: print(“it’s odd”) print(“good!”)
 
 print(“thank you”) print(“all done”) check_oddness() block of code inside “if” block of code inside “else” block of code in
 check_oddness

slide-41
SLIDE 41

Code Blocks

Code: def check_oddness():
 x = input(“enter x: ”) x = int(x) if x % 2 == 0: print(“it’s even”)
 print(“we wanted odd”) else: print(“it’s odd”) print(“good!”)
 
 print(“thank you”) print(“all done”) check_oddness() block of code inside “if” block of code inside “else” block of code in
 check_oddness You need to get good at “seeing” code blocks in Python code.


slide-42
SLIDE 42

Code Blocks

Code: def check_oddness():
 x = input(“enter x: ”) x = int(x) if x % 2 == 0: print(“it’s even”)
 print(“we wanted odd”) else: print(“it’s odd”) print(“good!”)
 
 print(“thank you”) print(“all done”) check_oddness() block of code inside “if” block of code inside “else” block of code in
 check_oddness You need to get good at “seeing” code blocks in Python code.
 Even blocks inside blocks inside blocks…

slide-43
SLIDE 43

Identifying Code Blocks

Code: def check_oddness():
 x = input(“enter x: ”) x = int(x) if x % 2 == 0: print(“it’s even”)
 print(“we wanted odd”) else: print(“it’s odd”) print(“good!”)
 
 print(“thank you”) print(“all done”) check_oddness()

slide-44
SLIDE 44

Identifying Code Blocks

Code: def check_oddness():
 x = input(“enter x: ”) x = int(x) if x % 2 == 0: print(“it’s even”)
 print(“we wanted odd”) else: print(“it’s odd”) print(“good!”)
 
 print(“thank you”) print(“all done”) check_oddness() Step 1: look for a colon at
 end of a line

slide-45
SLIDE 45

Identifying Code Blocks

Code: def check_oddness():
 x = input(“enter x: ”) x = int(x) if x % 2 == 0: print(“it’s even”)
 print(“we wanted odd”) else: print(“it’s odd”) print(“good!”)
 
 print(“thank you”) print(“all done”) check_oddness() Step 2: start drawing a line


  • n next code line, indented in
slide-46
SLIDE 46

Identifying Code Blocks

Code: def check_oddness():
 x = input(“enter x: ”) x = int(x) if x % 2 == 0: print(“it’s even”)
 print(“we wanted odd”) else: print(“it’s odd”) print(“good!”)
 
 print(“thank you”) print(“all done”) check_oddness() Step 3: continue down until you hit
 code that is less indented

slide-47
SLIDE 47

Identifying Code Blocks

Code: def check_oddness():
 x = input(“enter x: ”) x = int(x) if x % 2 == 0: print(“it’s even”)
 print(“we wanted odd”) else: print(“it’s odd”) print(“good!”)
 
 print(“thank you”) print(“all done”) check_oddness() Step 4: box off the code

slide-48
SLIDE 48

Identifying Code Blocks

Code: def check_oddness():
 x = input(“enter x: ”) x = int(x) if x % 2 == 0: print(“it’s even”)
 print(“we wanted odd”) else: print(“it’s odd”) print(“good!”)
 
 print(“thank you”) print(“all done”) check_oddness() Step 4: box off the code

slide-49
SLIDE 49

Identifying Code Blocks

Code: def check_oddness():
 x = input(“enter x: ”) x = int(x) if x % 2 == 0: print(“it’s even”)
 print(“we wanted odd”) else: print(“it’s odd”) print(“good!”)
 
 print(“thank you”) print(“all done”) check_oddness() to find more boxes,
 look for the next colon
 and repeat

slide-50
SLIDE 50

Identifying Code Blocks

Code: def check_oddness():
 x = input(“enter x: ”) x = int(x) if x % 2 == 0: print(“it’s even”)
 print(“we wanted odd”) else: print(“it’s odd”) print(“good!”)
 
 print(“thank you”) print(“all done”) check_oddness() to find more boxes,
 look for the next colon
 and repeat

slide-51
SLIDE 51

Identifying Code Blocks

Code: def check_oddness():
 x = input(“enter x: ”) x = int(x) if x % 2 == 0: print(“it’s even”)
 print(“we wanted odd”) else: print(“it’s odd”) print(“good!”)
 
 print(“thank you”) print(“all done”) check_oddness() to find more boxes,
 look for the next colon
 and repeat

slide-52
SLIDE 52

Identifying Code Blocks

Code: def check_oddness():
 x = input(“enter x: ”) x = int(x) if x % 2 == 0: print(“it’s even”)
 print(“we wanted odd”) else: print(“it’s odd”) print(“good!”)
 
 print(“thank you”) print(“all done”) check_oddness() to find more boxes,
 look for the next colon
 and repeat

slide-53
SLIDE 53

Identifying Code Blocks

Code: def check_oddness():
 x = input(“enter x: ”) x = int(x) if x % 2 == 0: print(“it’s even”)
 print(“we wanted odd”) else: print(“it’s odd”) print(“good!”)
 
 print(“thank you”) print(“all done”) check_oddness() to find more boxes,
 look for the next colon
 and repeat Do practice problems on worksheet

slide-54
SLIDE 54

Today's Outline

Review Control Flow Diagrams Basic syntax for “if” Identifying code blocks Demos