Sep 09: 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 nestedif
statements. - Use a debugger to step through and examine programs one line at a time.
Reminders¶
[5 min]
- Due tomorrow
- Quiz 1 on Wednesday
- Based on HW 1 and HW 2
- Review questions from Practice Quiz 1
- Study the challenge activities in zyBook
- If statements not allowed
- Required event: First Year Kickoff 9/11
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
- POGIL Role Cards (used in today's Meta Activity)
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)