Neural Sync Active
Programming in Python · Week 3 — Conditionals continued
Registry Synced
Programming in Python · Week 3 — Conditionals continued
1311 words
7 min read
2026-08-16
Reading compass
Now · Week map
Week 3 — conditionals continued
Quiz 2 scope: Weeks 1–8 per IITM May 2026 foundation courses. Source baseline: IITM BS admissions important-dates calendar · May 2026 cycle. Times on assessments are operational conventions — verify hall ticket.
Week map
Compound predicates → nested if → decision table → de Morgan
Classify → Represent → Execute → Trap-check
- Recognize: Ask: When does
A or Bshort-circuit? - Procedure: Evaluate and/or left to right with short-circuit:
andstops at first false;orstops at first true. Parentheses override default grouping. - Variations / traps: Watch for: and/or confused with mathematical inequalities.
Formula chain (compressed)
nested if → decision table → and/or precedence → elif ladder order.
- Nested if —
if A: if B: ...— compound conditions - and/or precedence —
and before or— use parentheses when unsure - elif chain —
first true branch wins— mutually exclusive cases - Boolean ops —
not, and, or— combine predicates - Decision table —
rows = cases, cols = outcomes— trace before coding
Open interactive formula desk · Week 3 tab.
Deep study
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 < 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?
ChatGPT prep archive
Archived import for extra depth — complements the notes above, not official IITM material.
Core concepts
- and / or / not: and needs both true; or needs at least one; not flips.
- Nested if: inner test only when outer is true; scope of each block matters.
- Decision table: rows are cases; columns are conditions; one action column.
- De Morgan: not (A and B) equals not A or not B; not (A or B) equals not A and not B.
Notation & vocabulary
| Operator | True when |
|---|---|
A and B | both A and B true |
A or B | at least one true |
not A | A is false |
Pattern families
Easy — Compound predicate
Evaluate and/or left to right with short-circuit:
and stops at first false; or stops at first true. Parentheses override default grouping.Medium — Nested branch trace
Outer false skips entire inner block. Draw indentation levels on paper. Each level has its own else paired with nearest if.
Hard — Decision table to code
List mutually exclusive cases. Encode as if/elif chain or nested structure. Verify every input row maps to exactly one outcome; add else for uncovered cases.
Drill these on the pattern atlas — filter to week 3.
Traps
- and/or confused with mathematical inequalities.
- Dangling else attaching to wrong if.
- not (x > 0 and x < 10) mishandled without de Morgan.
- Treating or as exclusive (Python or allows both true).
Retrieval prompts
- When does
A or Bshort-circuit? - Rewrite
not (p and q)without not on and. - How does nesting change which else runs?
Practice loop
- Read Deep study (if present) or core concepts once.
- Recite the formula chain without looking.
- Open one easy pattern on the interactive atlas for week 3.
- Attempt without solutions; mark studied after an honest try.
- Say one trap aloud before closing the tab.