[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
[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 • 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
Today's Outline Review Control Flow Diagrams Basic syntax for “if” Identifying code blocks Demos
Indentation Example what does it print? print(“A”) print(“B”) def print_letters (): print(“C”) print(“D”) print(“E”) print(“F”) print_letters()
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()
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()
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()
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()
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()
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()
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).
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).
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()
Today's Outline Review Control Flow Diagrams Basic syntax for “if” Identifying code blocks Demos
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”)
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”)
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”)
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”)
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”)
“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”)
“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”)
Today's Outline Review Control Flow Diagrams Basic syntax for “if” Identifying code blocks Demos
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”)
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”)
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”)
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”)
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”)
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”)
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”)
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”)
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”)
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”)
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”)
Today's Outline Review Control Flow Diagrams Basic syntax for “if” Identifying code blocks Demos
Recommend
More recommend
Explore More Topics
Stay informed with curated content and fresh updates.