Quiz 2

732 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 3 — Nested conditionals Compound predicates and nested blocks combine simple tests into **structured decisions** — the quiz tests scope, pairing, and logical operators. ## Week map `and` / `or` / `not` → short-circuit evaluation → nested `if` → indentation scope → de Morgan rewrites →...

Programming in Python · Week 3 — Nested conditionals

Compound predicates and nested blocks combine simple tests into structured decisions — the quiz tests scope, pairing, and logical operators.

Week map

and / or / not → short-circuit evaluation → nested if → indentation scope → de Morgan rewrites → decision tables → exclusive cases.

Logical operator notation

  • A and B → both must be true → x > 0 and x < 10 true only inside (0,10).
  • A or B → at least one true → day == "Sat" or day == "Sun for weekend.
  • not A → negation → not (x == 5) same as x != 5 for values.
  • Short-circuit: False and f() does not call f(); True or g() skips g().

Truth tables (mini)

ABA and BA or B
TTTT
TFFT
FTFT
FFFF

De Morgan’s laws

  • not (A and B)not A or not B
  • not (A or B)not A and not B
Mini-example: “not (teen and student)” → not teen or not student.
Useful for simplifying negated compound conditions without bracket errors.

Nested if structure

Outer if false → entire inner block skipped.
python
x = 6
y = 2
if x > 5:
    if y > 5:
        print("both")
    else:
        print("outer only")
else:
    print("neither")
Prints outer only because inner y > 5 fails.
Else pairing: else attaches to the nearest unmatched if at same indentation.

Pattern families

Easy — Compound predicate

  • Evaluate and/or on two compares.
  • Predict result of not on simple inequality.
  • Short-circuit: second operand may not run.

Medium — Nested branch trace

  • Two-level nesting with prints in each block.
  • Identify which else runs.
  • Convert word problem “if A and B then … else if C …” to code shape.

Hard — Decision table to code

  • Mutually exclusive cases as if / elif chain.
  • Negated compound conditions via de Morgan.
  • Equivalent rewrites: nested if vs single compound predicate.

Worked mini-examples

Example 1 — and gate.
python
temp = 22
humid = 80
if temp > 18 and humid < 70:
    comfort = "dry warm"
else:
    comfort = "other"
# comfort = "other" (humid fails)
Example 2 — or weekend.
python
d = "Sat"
if d == "Fri" or d == "Sat" or d == "Sun":
    kind = "off"
else:
    kind = "work"
# kind = "off"
Example 3 — de Morgan.
python
age = 16
licensed = False
# not (age >= 18 and licensed)
if not (age >= 18 and licensed):
    can_drive = False
else:
    can_drive = True
# can_drive False
Example 4 — nested.
python
a, b = 3, 3
if a > 0:
    if b > 0:
        sign = "++"
    else:
        sign = "+-"
else:
    sign = "-"
# sign = "++"

Traps

  • and confused with “between” — need two inequalities: low <= x and x <= high.
  • Dangling else paired with wrong if after editing indentation.
  • or is inclusive — both true still satisfies or.
  • Negating without parentheses: not x > 5 parses as (not x) > 5 — wrong; use not (x > 5).
  • Nested structure duplicated work — two separate if x>0 and if y>0 differ from if x>0 and y>0.

Diagnostic (try yourself)

  1. What is result?
python
n = 7
if n % 2 == 0 and n > 5:
    result = "A"
elif n % 2 == 1 and n > 5:
    result = "B"
else:
    result = "C"
  1. Rewrite without not ( ... and ... ) using de Morgan:
python
if not (x < 0 or x > 100):
    ok = True
  1. What prints?
python
p, q = 2, 10
if p > 0:
    if q < 5:
        print(1)
    else:
        print(2)
else:
    print(3)
  1. For integers a and b, write one if condition that is true exactly when both are positive.
  2. if True or expensive(): — is expensive() called? Why?
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.