301 conditions
play

[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


  1. [301] Conditions Based on slides created by Tyler Caraza-Harter

  2. Learning Objectives Today Reason about conditions • Conditional execution Chapter 5 of Think Python • Alternate execution (skip “Recursion” sections) • 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

  3. Today's Outline Review Control Flow Diagrams Basic syntax for “if” Identifying code blocks Demos

  4. 
 Indentation Example what does it print? print(“A”) 
 print(“B”) 
 def print_letters (): 
 print(“C”) 
 print(“D”) print(“E”) 
 print(“F”) print_letters()

  5. 
 Indentation Example what does it print? print(“A”) 
 A 
 print(“B”) 
 B 
 def print_letters (): 
 E 
 print(“C”) 
 print(“D”) F 
 C 
 print(“E”) 
 print(“F”) D print_letters()

  6. 
 Indentation Example what does it print? print(“A”) 
 A 
 print(“B”) 
 B 
 def print_letters (): 
 E 
 print(“C”) 
 indented, so “inside” 
 print_letters function print(“D”) F 
 C 
 print(“E”) 
 print(“F”) D print_letters()

  7. 
 Indentation Example what does it print? print(“A”) 
 A 
 print(“B”) 
 B 
 def print_letters (): 
 E 
 print(“C”) 
 indented, so “inside” 
 print_letters function print(“D”) F 
 C 
 print(“E”) 
 printed last because 
 print(“F”) print_letters is called last D print_letters()

  8. 
 Indentation Example what does it print? print(“A”) 
 A 
 print(“B”) 
 B 
 def print_letters (): 
 E 
 print(“C”) 
 indented, so “inside” 
 print_letters function print(“D”) F 
 C 
 print(“E”) 
 print(“F”) D print_letters()

  9. 
 Indentation Example what does it print? not indented, so print(“A”) 
 A 
 “outside” any function print(“B”) 
 B 
 def print_letters (): 
 E 
 print(“C”) 
 indented, so “inside” 
 print_letters function print(“D”) F 
 C 
 print(“E”) 
 print(“F”) D print_letters()

  10. 
 Indentation Example what does it print? not indented, so print(“A”) 
 A 
 “outside” any function print(“B”) 
 B 
 def print_letters (): 
 E 
 print(“C”) 
 indented, so “inside” 
 print_letters function print(“D”) F 
 C 
 also not indented, so print(“E”) 
 “outside” any function. print(“F”) D Runs BEFORE 
 print_letters is called print_letters()

  11. 
 Indentation Example what does it print? not indented, so print(“A”) 
 A 
 “outside” any function print(“B”) 
 B 
 def print_letters (): 
 E 
 print(“C”) 
 indented, so “inside” 
 print_letters function print(“D”) F 
 C 
 also not indented, so print(“E”) 
 “outside” any function. print(“F”) D Runs BEFORE 
 print_letters is called print_letters() We use indenting to tell Python which code is inside or outside 
 of a function (or other things we’ll learn about soon).

  12. 
 Indentation Example what does it print? not indented, so print(“A”) 
 A 
 “outside” any function print(“B”) 
 B 
 def print_letters (): 
 E 
 print(“C”) 
 indented, so “inside” 
 print_letters function print(“D”) F 
 blank lines are irrelevant C 
 also not indented, so print(“E”) 
 “outside” any function. print(“F”) D Runs BEFORE 
 print_letters is called print_letters() We use indenting to tell Python which code is inside or outside 
 of a function (or other things we’ll learn about soon).

  13. 
 Indentation Example what does it print? print(“A”) 
 A 
 print(“B”) 
 B 
 def print_letters (): 
 E 
 print(“C”) 
 we’ll often call the lines 
 print(“D”) of code inside something 
 F 
 a “block” of code C 
 print(“E”) 
 print(“F”) D print_letters()

  14. Today's Outline Review Control Flow Diagrams Basic syntax for “if” Identifying code blocks Demos

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

  16. Control Flow Diagrams x = input(“enter x: ”) x = int(x) x % 2 == 0 False True Sometimes 
 we do this print(“it’s odd”) print(“it’s even”) print(“thank you”)

  17. Control Flow Diagrams x = input(“enter x: ”) x = int(x) x % 2 == 0 False True Sometimes 
 Other times 
 we do this we do this print(“it’s odd”) print(“it’s even”) print(“thank you”)

  18. Control Flow Diagrams x = input(“enter x: ”) x = int(x) condition x % 2 == 0 False True Sometimes 
 Other times 
 we do this we do this print(“it’s odd”) print(“it’s even”) print(“thank you”)

  19. Control Flow Diagrams x = input(“enter x: ”) x = int(x) boolean expressions are mostly 
 used for deciding what to do next 
 condition (not for printing “True” or “False” 
 is in most of our examples thus far) x % 2 == 0 False True Sometimes 
 Other times 
 we do this we do this print(“it’s odd”) print(“it’s even”) print(“thank you”)

  20. “Paths of Execution” Input/Output: enter x: 7 x = input(“enter x: ”) it’s odd x = int(x) thank you x % 2 == 0 False True print(“it’s odd”) print(“it’s even”) print(“thank you”)

  21. “Paths of Execution” Input/Output: enter x: 8 x = input(“enter x: ”) it’s even x = int(x) thank you x % 2 == 0 False True print(“it’s odd”) print(“it’s even”) print(“thank you”)

  22. Today's Outline Review Control Flow Diagrams Basic syntax for “if” Identifying code blocks Demos

  23. Writing conditions in Python Code: x = input(“enter x: ”) x = int(x) x % 2 == 0 False True print(“it’s odd”) print(“it’s even”) print(“thank you”)

  24. Writing conditions in Python Code: x = input(“enter x: ”) x = int(x) x = input(“enter x: ”) x = int(x) x % 2 == 0 False True print(“it’s odd”) print(“it’s even”) print(“thank you”)

  25. Writing conditions in Python Code: x = input(“enter x: ”) x = int(x) x = input(“enter x: ”) x = int(x) if x % 2 == 0: x % 2 == 0 False True print(“it’s odd”) print(“it’s even”) print(“thank you”)

  26. Writing conditions in Python Code: x = input(“enter x: ”) x = int(x) x = input(“enter x: ”) x = int(x) if x % 2 == 0: x % 2 == 0 False True print(“it’s odd”) print(“it’s even”) boolean expression print(“thank you”)

  27. Writing conditions in Python Code: x = input(“enter x: ”) x = int(x) x = input(“enter x: ”) x = int(x) if x % 2 == 0: x % 2 == 0 print(“it’s even”) False True print(“it’s odd”) print(“it’s even”) print(“thank you”)

  28. 
 Writing conditions in Python Code: x = input(“enter x: ”) x = int(x) x = input(“enter x: ”) x = int(x) if x % 2 == 0: x % 2 == 0 print(“it’s even”) else: False True print(“it’s odd”) 
 print(“it’s odd”) print(“it’s even”) print(“thank you”)

  29. 
 Writing conditions in Python Code: x = input(“enter x: ”) x = int(x) x = input(“enter x: ”) x = int(x) if x % 2 == 0: x % 2 == 0 print(“it’s even”) else: False True print(“it’s odd”) 
 print(“thank you”) print(“it’s odd”) print(“it’s even”) print(“thank you”)

  30. 
 Writing conditions in Python Code: x = input(“enter x: ”) x = int(x) x = input(“enter x: ”) x = int(x) if x % 2 == 0: x % 2 == 0 print(“it’s even”) else: False True print(“it’s odd”) 
 print(“thank you”) print(“it’s odd”) print(“it’s even”) print(“thank you”)

  31. 
 Writing conditions in Python Code: x = input(“enter x: ”) x = int(x) x = input(“enter x: ”) x = int(x) if x % 2 == 0: x % 2 == 0 print(“it’s even”) else: False True print(“it’s odd”) print(“good!”) 
 print(“it’s odd”) print(“it’s even”) print(“good!”) print(“thank you”) print(“thank you”)

  32. 
 Writing conditions in Python Code: x = input(“enter x: ”) x = int(x) x = input(“enter x: ”) x = int(x) if x % 2 == 0: x % 2 == 0 print(“it’s even”) 
 print(“we wanted odd”) False True else: print(“it’s odd”) print(“it’s odd”) print(“it’s even”) 
 print(“good!”) 
 print(“good!”) print(“we wanted odd”) print(“thank you”) print(“thank you”)

  33. 
 Writing conditions in Python Code: x = input(“enter x: ”) x = int(x) x = input(“enter x: ”) x = int(x) if x % 2 == 0: x % 2 == 0 print(“it’s even”) 
 print(“we wanted odd”) False True else: print(“it’s odd”) print(“it’s odd”) print(“it’s even”) 
 print(“good!”) 
 print(“good!”) print(“we wanted odd”) print(“thank you”) print(“all done”) print(“thank you”) print(“all done”)

  34. Today's Outline Review Control Flow Diagrams Basic syntax for “if” Identifying code blocks Demos

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend