Neural Sync Active
python-week2
Registry Synced
python-week2
744 words
4 min read
Reading compass
Now · Week map
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?