Quiz 2

Programming in Python · Week 2 — Conditionals

1308 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

if/elif/else, predicates, truthiness — concepts, pattern families, and traps for Quiz 2 week 2. # Week 2 — conditionals > **Quiz 2 scope:** Weeks 1–8 per IITM May 2026 foundation courses.

Week 2 — conditionals

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

Predicate → branch choice → elif chain → else default

Classify → Represent → Execute → Trap-check

  • Recognize: Ask: When does elif run relative to if?
  • Procedure: Evaluate only until first true predicate. Skip remaining elif/else. If none true, only else runs—or no branch if else missing.
  • Variations / traps: Watch for: elif after a true if still evaluated (it is skipped).

Formula chain (compressed)

predicate → if/elif/else (first true wins) → and/or short-circuit.
  1. Branchif cond: ... elif: ... else: — at most one block runs
  2. andA and B — both must be true
  3. orA or B — at least one true
  4. Equality== vs = — compare vs assign
  5. Truthiness0, "", [], None → falsy — if without explicit bool

Deep study

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?

ChatGPT prep archive

Archived import for extra depth — complements the notes above, not official IITM material.

Core concepts

  • if / elif / else: exactly one branch runs when predicates are tested in order.
  • Predicate: expression with bool result; comparisons and ==, !=, <, >, <=, >=.
  • Truthiness: empty string, 0, empty list are falsy; most other values truthy.
  • Indentation defines block body—must be consistent.

Notation & vocabulary

ConstructRole
if cond:first test
elif cond:alternate test
else:fallback when all tests false

Pattern families

Easy — Which branch runs?

Evaluate only until first true predicate. Skip remaining elif/else. If none true, only else runs—or no branch if else missing.

Medium — Boundary comparisons

Check inclusive vs exclusive bounds carefully. Chain of elif is not independent if-tests; earlier true branch blocks later ones even if they would also be true.

Hard — Truthiness trap

Nonzero numbers and non-empty strings are truthy. if x: differs from if x == True. Empty input string may be valid data—decide policy before condensing to truthiness.
Drill these on the pattern atlas — filter to week 2.

Traps

  • elif after a true if still evaluated (it is skipped).
  • Confusing is with == for value equality.
  • Missing else when all paths must assign a variable.
  • Off-by-one on inclusive <= vs exclusive <.

Retrieval prompts

  1. When does elif run relative to if?
  2. Which values are falsy in Python?
  3. What prints if all if/elif tests fail and there is no else?

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 2.
  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.