Skip to content

Sep 11: More Logic, Tracing Code

Learning Objectives

After today's class, you should be able to:

  • Summarize what values Python considers to be true in a Boolean context.
  • Predict output when using if, elif, else, and nested if statements.
  • Use a debugger to step through and examine programs one line at a time.

Reminders

[5 min]

  • Due tomorrow
  • Quiz 1b on Wednesday
    • Will replace your Quiz1 score if higher

Mini Activity

[25 min]

  • Boolean Values
    • If you are absent today, complete this activity at home
    • Bring your completed activity to class or office hours
  • Optional: To learn more about POGIL, check out pogil.org

Code snippet:

value = 123
if value:
    print("Truthy")
else:
    print("Falsy")

Tracing Practice

[20 min]

For each example:

  • Predict the output as a team (by hand, on paper)
  • Step through the code using Thonny's debugger

Example 1:

hat = "fedora"

if hat == "fedora":
    print("A")
else:
    print("B")

print("C")

Example 2:

x = 10

if x < 100:
    print("A")
elif x < 50:
    print("B")
else:
    print("C")

print("D")

Example 3:

x = 10

if x < 100:
    print("A")
if x < 50:
    print("B")

print("C")

Example 4:

x = 10
hat = "fez"

if x > 100 or hat == "fez":
    print("A")
    if x < 50:
        print("B")
    else:
        print("C")
else:
    print("D")
    if hat == "fedora":
        print("E")
print("F")

Example 5:

x = 1.0
y = 0.0
b1 = x > 0 and y > 0
b2 = not x <= 0 or y <= 0
b3 = not (x <= 0 or y <= 0)
print(b1, b2, b3)