Quiz 2

Programming in Python · Week 3 — Conditionals continued

1311 words
7 min read
2026-08-16T00:00:00.000Z
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

nested branches, and/or, decision tables — concepts, pattern families, and traps for Quiz 2 week 3. # Week 3 — conditionals continued > **Quiz 2 scope:** Weeks 1–8 per IITM May 2026 foundation courses.

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.
Part of the Quiz 2 prep system%20%C2%B7%20%5BWeeks%201%E2%80%938%20index%5D(.%2Fmay-2026-python-quiz-2-weeks-1-8-prep) · Pattern atlas · Formula chains.

Week map

Compound predicates → nested if → decision table → de Morgan

Classify → Represent → Execute → Trap-check

  • Recognize: Ask: When does A or B short-circuit?
  • Procedure: Evaluate and/or left to right with short-circuit: and stops at first false; or stops 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.
  1. Nested ifif A: if B: ... — compound conditions
  2. and/or precedenceand before or — use parentheses when unsure
  3. elif chainfirst true branch wins — mutually exclusive cases
  4. Boolean opsnot, and, or — combine predicates
  5. Decision tablerows = cases, cols = outcomes — trace before coding

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 < 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?

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

OperatorTrue when
A and Bboth A and B true
A or Bat least one true
not AA 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

  1. When does A or B short-circuit?
  2. Rewrite not (p and q) without not on and.
  3. How does nesting change which else runs?

Practice loop

  1. Read Deep study (if present) or core concepts once.
  2. Recite the formula chain without looking.
  3. Open one easy pattern on the interactive atlas for week 3.
  4. Attempt without solutions; mark studied after an honest try.
  5. Say one trap aloud before closing the tab.
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.