What this topic tests
- Order if and elif branches so the broadest condition is not first.
- Predict truthiness for empty collections, zero, and None.
- Use comparison chains and boolean operators without ambiguity.
Common mistakes
- Using = when you meant ==.
- Putting a broad if before a more specific elif, which makes the elif unreachable.
- Misreading indentation and assuming a statement belongs to the branch above it.
Sample questions
Ten representative questions from the full bank. Try each one before opening the answer.
-
1. Ming only makes milk tea when there is both milk and tea. What is True and False?
Show answer
Answer
B. FalseWhy: and returns False as soon as one side is false and does not evaluate the rest. Both conditions must hold, so the result is False.
-
2. 🧋 Ming only makes milk tea when there is milk and tea. What is the value of True and 0?
Show answer
Answer
B. 0Why: and returns the first falsy operand, and 0 is falsy, so the result is 0 rather than False. Truthiness and identity are different things.
-
3. 🧋 Which option is equivalent to the conditional expression a if cond else b?
Show answer
Answer
A. an if/else block that chooses a or bWhy: The conditional expression is shorthand for an if/else that picks a or b; the ? : form belongs to C-family languages, not Python.
-
4. If the condition is `if 0:`, which branch runs?
Show answer
Answer
A. The else branchWhy: if 0 tests truthiness, and 0 is false, so the if body is skipped and the else branch runs; nothing raises an error here. An empty list or empty string behaves the same way.
-
5. Ming checks whether “boba” is in the topping list ["boba", "coconut jelly", "pudding"]: "boba" ___ ["boba", "coconut jelly", "pudding"]
Show answer
Answer
inWhy: in tests membership and returns a bool; boba is one of the three toppings, so the check is True. not in is its negation. Membership always answers with a bool.
-
6. 🧋 Ming checks the milk-and-tea conditions. Converting the result of `1 and 2` to bool gives ___.
Show answer
Answer
TrueWhy: 1 and 2 returns 2 because both operands are truthy, and bool(2) is True. The value and its truthiness are separate questions.
-
7. 🧋 Ming checks whether a drink is on the menu. Test whether the key 'age' is in dictionary d: 'age' ___ d
Show answer
Answer
inWhy: For a dict, in checks keys rather than values, so 'age' in d asks whether that key exists; use d.values() to test values instead.
-
8. How is 0 treated in a conditional expression, and what is printed?
x = 0 print("yes" if x else "no")Show answer
Answer
noWhy: Truthiness treats 0, an empty string, an empty list and None as false, so the else value is chosen. Only a truthy x would print yes.
-
9. What does `score(-5, 0)` return?
from math import sqrt # X, and Y variable names against [pylint]: C0104, but is the same as the stub, advise not to change this. def score(x, y): dart_location = sqrt(x * x + y * y) if dart_location <= 1.0: return 10 elif dart_location <= 5.0: return 5 elif dart_location <= 10.0: return 1 else: return 0 print(score(-5, 0))Show answer
Answer
5Why: sqrt(25 + 0) = 5.0, above 1.0 but within 5.0, so the second branch returns 5. The boundary is inclusive because the test is <=.
-
10. What does `equilateral([0.5, 0.5, 0.5])` return?
def valid(sides): return sum(sorted(sides)[:2]) >= sorted(sides)[2] and all( side > 0 for side in sides ) def equilateral(sides): return valid(sides) and all(sides[0] == side for side in sides) def isosceles(sides): return valid(sides) and any( side_1 == side_2 for side_1, side_2 in zip(sorted(sides), sorted(sides)[1:]) ) def scalene(sides): return valid(sides) and not isosceles(sides) print(equilateral([0.5, 0.5, 0.5]))Show answer
Answer
TrueWhy: Floats work the same way: 0.5 + 0.5 >= 0.5 and all sides are equal, so equilateral returns True. Nothing restricts the check to integers.
Common questions
Is elif the same as else followed by if?
Logically it is similar, but elif keeps the branches mutually exclusive and avoids extra indentation.
What values are false?
False, None, numeric zero, and empty strings or collections are false. Most other objects are true.
Keep the streak going.
The full bank keeps your place in this browser, filters by topic, and includes a daily question and mistake review.