Neural Sync Active
python-week3
Registry Synced
python-week3
732 words
4 min read
Reading compass
Now · Week map
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 < 10true only inside (0,10).A or B→ at least one true →day == "Sat" or day == "Sunfor weekend.not A→ negation →not (x == 5)same asx != 5for values.- Short-circuit:
False and f()does not callf();True or g()skipsg().
Truth tables (mini)
| A | B | A and B | A or B |
|---|---|---|---|
| T | T | T | T |
| T | F | F | T |
| F | T | F | T |
| F | F | F | F |
De Morgan’s laws
not (A and B)≡not A or not Bnot (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.pythonx = 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/oron two compares. - Predict result of
noton simple inequality. - Short-circuit: second operand may not run.
Medium — Nested branch trace
- Two-level nesting with prints in each block.
- Identify which
elseruns. - Convert word problem “if A and B then … else if C …” to code shape.
Hard — Decision table to code
- Mutually exclusive cases as
if/elifchain. - Negated compound conditions via de Morgan.
- Equivalent rewrites: nested
ifvs single compound predicate.
Worked mini-examples
Example 1 — and gate.
pythontemp = 22 humid = 80 if temp > 18 and humid < 70: comfort = "dry warm" else: comfort = "other" # comfort = "other" (humid fails)
Example 2 — or weekend.
pythond = "Sat" if d == "Fri" or d == "Sat" or d == "Sun": kind = "off" else: kind = "work" # kind = "off"
Example 3 — de Morgan.
pythonage = 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.
pythona, b = 3, 3 if a > 0: if b > 0: sign = "++" else: sign = "+-" else: sign = "-" # sign = "++"
Traps
andconfused with “between” — need two inequalities:low <= x and x <= high.- Dangling
elsepaired with wrongifafter editing indentation. oris inclusive — both true still satisfiesor.- Negating without parentheses:
not x > 5parses as(not x) > 5— wrong; usenot (x > 5). - Nested structure duplicated work — two separate
if x>0andif y>0differ fromif x>0 and y>0.
Diagnostic (try yourself)
- What is
result?
pythonn = 7 if n % 2 == 0 and n > 5: result = "A" elif n % 2 == 1 and n > 5: result = "B" else: result = "C"
- Rewrite without
not ( ... and ... )using de Morgan:
pythonif not (x < 0 or x > 100): ok = True
- What prints?
pythonp, q = 2, 10 if p > 0: if q < 5: print(1) else: print(2) else: print(3)
-
For integers
aandb, write oneifcondition that is true exactly when both are positive. -
if True or expensive():— isexpensive()called? Why?