Programming in Python · Week 2 — Conditionals
1308 words
7 min read
2026-08-16T00:00:00.000Z
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.
- Branch —
if cond: ... elif: ... else:— at most one block runs - and —
A and B— both must be true - or —
A or B— at least one true - Equality —
== vs =— compare vs assign - Truthiness —
0, "", [], None → falsy— if without explicit bool
Open interactive formula desk · Week 2 tab.
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 whencondisTrue.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
| Op | Meaning | Example true when |
|---|---|---|
== | equal value | 5 == 5 |
!= | not equal | 3 != 7 |
<, > | strict less/greater | 2 < 9 |
<=, >= | inclusive | 4 <= 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:
pythonscore = 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
- Evaluate
ifcondition. If true, run its block and skip allelifandelse. - Else evaluate first
elif. If true, run it and skip rest. - If none true, run
elseif present; if noelse, no block runs.
pythonn = 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/elsewith numeric compare. - Predict print output from one predicate.
- Identify when
elseruns.
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:vsif 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.
pythonx = 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.
pythonk = 3 if k > 5: t = 1 # no else # t may not exist — NameError if referenced
Example 3 — Boundary.
pythonage = 18 if age >= 18: status = "adult" else: status = "minor" # status = "adult" (18 counts)
Example 4 — Truthiness.
pythonname = "" if name: print("hi") else: print("empty") # prints empty
Traps
elifstill evaluated mentally whenifwas true — it is skipped entirely.- Off-by-one on inclusive bounds.
- Assigning in one branch but using variable assuming another branch ran.
== Truewith integers: only1equalsTrueoddly; preferif flag:orif flag is Truerarely.- Dangling logic: two separate
ifstatements both may run — different fromif/elif.
Diagnostic (try yourself)
- What is printed?
pythonv = 12 if v > 15: print("X") elif v > 8: print("Y") else: print("Z")
- After this code, what is
w?
pythonw = 5 if w < 3: w = 10 elif w < 7: w = 20 else: w = 30
- What is printed?
pythonflag = 0 if flag: print("on") else: print("off")
-
Rewrite using only
ifandelse(noelif): assign"pass"whenscore >= 40, else"fail". Variablescorealready exists. -
Two separate
ifstatements both testx > 0. First prints"A", second prints"B". Ifx = 5, what prints? Ifx = -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
| Construct | Role |
|---|---|
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
iswith==for value equality. - Missing else when all paths must assign a variable.
- Off-by-one on inclusive
<=vs exclusive<.
Retrieval prompts
- When does elif run relative to if?
- Which values are falsy in Python?
- What prints if all if/elif tests fail and there is no else?
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 2.
- Attempt without solutions; mark studied after an honest try.
- Say one trap aloud before closing the tab.