Quiz 2

744 words
4 min read
Python Week 1: the first filter for runtime behavior
Visual companion
Python
Type and operator map

Python Week 1: the first filter for runtime behavior

View
Revision summary

What this note is really saying

Short form

# Programming in Python · Week 2 — Conditionals Branching chooses **one path** based on predicates. Quiz items reward exact knowledge of evaluation order and truth values.

Programming in Python · Week 2 — Conditionals

Branching chooses one path based on predicates. Quiz items reward exact knowledge of evaluation order and truth values.

Week map

Boolean predicates → if / elif / else → first-match wins → truthiness → comparison operators → common boundary bugs.

Conditional notation

  • if cond: → conditional block → runs body only when cond is True.
  • elif cond: → else-if → tested only if all prior branches failed.
  • else: → fallback → runs when every test above was false.
  • cond → predicate → expression with boolean result → x >= 10, name == "Ada".

Comparison operators

OpMeaningExample true when
==equal value5 == 5
!=not equal3 != 7
<, >strict less/greater2 < 9
<=, >=inclusive4 <= 4
Trap: is tests identity (same object), not numeric equality for large ints in advanced contexts; week 2 quizzes usually want == for value compare.

Truthiness

In if x:, Python treats values as:
  • Falsy: 0, 0.0, False, None, "", [], {}, ().
  • Truthy: most other values, including nonzero numbers and non-empty strings.
if 3: runs. if "": skips.
Mini-example:
python
score = 0
if score:
    print("played")
else:
    print("skipped")
Prints skipped because 0 is falsy — even though zero may be a valid score in real life.

Branch selection rules

  1. Evaluate if condition. If true, run its block and skip all elif and else.
  2. Else evaluate first elif. If true, run it and skip rest.
  3. If none true, run else if present; if no else, no block runs.
python
n = 14
if n < 10:
    label = "low"
elif n < 20:
    label = "mid"
else:
    label = "high"
# label is "mid"
n < 20 is true but never tested because n < 10 was false and n < 20 is the first true elif.

Pattern families

Easy — Which branch runs?

  • Single if / else with numeric compare.
  • Predict print output from one predicate.
  • Identify when else runs.

Medium — elif chains and boundaries

  • Inclusive vs exclusive thresholds (<= vs <).
  • Overlapping conditions — only first true branch executes.
  • Assign variable in branches; trace final value.

Hard — Truthiness and edge inputs

  • Empty string vs "0" string (truthy).
  • if x: vs if x == True (usually prefer explicit compare for clarity).
  • Multiple variables set in different branches — know which path ran.

Worked mini-examples

Example 1 — First match.
python
x = 25
if x > 30:
    print("A")
elif x > 20:
    print("B")
elif x > 10:
    print("C")
else:
    print("D")
Prints B only.
Example 2 — Missing else.
python
k = 3
if k > 5:
    t = 1
# no else
# t may not exist — NameError if referenced
Example 3 — Boundary.
python
age = 18
if age >= 18:
    status = "adult"
else:
    status = "minor"
# status = "adult" (18 counts)
Example 4 — Truthiness.
python
name = ""
if name:
    print("hi")
else:
    print("empty")
# prints empty

Traps

  • elif still evaluated mentally when if was true — it is skipped entirely.
  • Off-by-one on inclusive bounds.
  • Assigning in one branch but using variable assuming another branch ran.
  • == True with integers: only 1 equals True oddly; prefer if flag: or if flag is True rarely.
  • Dangling logic: two separate if statements both may run — different from if / elif.

Diagnostic (try yourself)

  1. What is printed?
python
v = 12
if v > 15:
    print("X")
elif v > 8:
    print("Y")
else:
    print("Z")
  1. After this code, what is w?
python
w = 5
if w < 3:
    w = 10
elif w < 7:
    w = 20
else:
    w = 30
  1. What is printed?
python
flag = 0
if flag:
    print("on")
else:
    print("off")
  1. Rewrite using only if and else (no elif): assign "pass" when score >= 40, else "fail". Variable score already exists.
  2. Two separate if statements both test x > 0. First prints "A", second prints "B". If x = 5, what prints? If x = -1, what prints?
Document outline

Keep your place and jump directly to a heading.

Table of Contents
System Normal // Awaiting Context

Intelligence Hub

Navigate the knowledge graph to generate context. The Hub adapts dynamically to surface backlinks, related notes, and metadata insights.