1.2.15 · D5Introduction to Programming (Python)
Question bank — if - elif - else — syntax, indentation rules
Reminder of the vocabulary we lean on:
- A condition is a boolean expression — something Python resolves to
TrueorFalse. - A block is the indented group of lines owned by a header line ending in
:. - A chain is one
if, then zero-or-moreelif, then optionalelse.
True or false — justify
True or false: In an if/elif/elif chain without an else, exactly one block always runs.
False. If every condition is
False, zero blocks run and the program simply continues past the chain. Only else guarantees a fallback.True or false: Adding an else makes the chain both exhaustive and mutually exclusive.
True. The conditions partition all cases — the first
True wins (exclusive), and else catches "none were true" (exhaustive), so exactly one block runs.True or false: elif and a fresh if on the next line behave identically.
False.
elif is checked only if all earlier conditions were False and short-circuits the chain; a separate if is always evaluated independently and can overwrite an earlier result.True or false: Python needs curly braces {} to know where a block ends, just like C.
False. Python uses indentation alone. The block ends when a line returns to the outer indentation level — no braces exist in the syntax.
True or false: You may write as many elif branches as you like.
True. Zero or more
elif are allowed; the only limits are one if at the start and at most one else at the end.True or false: The else line carries its own condition in parentheses.
False.
else never has a condition — it is the unconditional fallback. Writing else something: is a SyntaxError.True or false: If two elif conditions are both True, both their blocks run.
False. Python stops at the first
True condition; later true conditions in the same chain are never even tested.True or false: if 0: and if False: behave the same way.
True. By Truthiness in Python,
0 is falsy, so both skip the block. Any falsy value (0, "", [], None) acts like False here.True or false: Putting the else at a deeper indentation than its if still works as long as it's consistent.
False.
else must align exactly with its matching if/elif. Misaligned indentation gives a SyntaxError or attaches it to the wrong statement.Spot the error
if x = 5: — what breaks and why?
= is assignment, not comparison, so it can't sit in a condition → SyntaxError. Use ==, which asks "is equal to?".if score >= 40 with no trailing colon — what happens?
SyntaxError. The colon : is what opens the block; without it Python doesn't know a block follows.if a > 0:
print("positive")
``` — why does this fail? ::: The `print` is not indented, so the `if` header promises a block but none is given → `IndentationError: expected an indented block`.
A file mixes 4 spaces on one line and a tab on the next inside the same block. What error appears? ::: `TabError: inconsistent use of tabs and spaces`. Tabs and spaces are different characters even when they look identical on screen.
```python
if raining:
take_umbrella()
elif raining and cold:
take_coat()
``` — what's the logic bug? ::: The `elif` is **dead code**: if `raining` is true the first branch already wins, and if `raining` is false then `raining and cold` is also false. The second branch can never run.
```python
if score >= 80:
grade = "B"
if score >= 70:
grade = "C"
``` — for `score = 85`, what goes wrong? ::: Both are independent `if`s, so both run: `grade` becomes `"B"` then is **overwritten** to `"C"`. Using `elif` would stop after the first match.
```python
else:
cleanup()
``` written with no preceding `if` — valid? ::: No. `else` must belong to a preceding `if`/`elif` chain. Alone it's a `SyntaxError`.
---
## Why questions
Why does Python check conditions strictly top-to-bottom instead of picking the "best" match? ::: Because the language guarantees **order-of-appearance evaluation with short-circuit**: the first `True` condition wins. This makes behaviour predictable and lets you place the most specific test first.
Why should the tighter bound come first in a grade chain (`>=90` before `>=80`)? ::: Because `85 >= 80` is also `True`. If `>=80` came first it would catch a `95` too, so you'd never reach `>=90`. Ordering from strictest to loosest keeps each band correct.
Why is indentation "grammar" and not just style in Python? ::: Because the indentation level is the **only** signal Python uses to decide which lines belong to a block. Change the indentation and you change the program's meaning, not just its looks.
Why does `if items:` work even though `items` is a list, not a boolean? ::: By [[Truthiness in Python]], a non-empty list is truthy and an empty list is falsy. The condition needn't literally be `True`/`False`; any value's truthiness is used.
Why does combining conditions with `and`/`or` (from [[Logical operators and, or, not]]) sometimes let you delete an `elif`? ::: A single branch with `a and b` can express what a nested `if` inside an `if` did, flattening the structure while keeping the same logic.
Why is a clean `if/elif/else` chain called "mutually exclusive"? ::: Because reaching any `elif` means all earlier conditions were false, so at most one block's condition can be the first true one — the branches cannot both fire.
Why can't `elif` appear before its `if`? ::: `elif` means "else, if" — it only has meaning as a continuation of an existing chain. Without a preceding `if` there is nothing for it to be the "else" of.
---
## Edge cases
What runs in `if False: ... elif False: ... else: ...`? ::: The `else` block runs, because it is the fallback for "every condition above was `False`".
What runs when the very first `if` condition is `True` but a later `elif` is *also* true? ::: Only the first block. Python never evaluates the later `elif` once a condition has matched.
For `if x:` where `x = ""` (empty string), does the block run? ::: No. An empty string is falsy, so the condition is treated as `False` and the block is skipped.
For `if x:` where `x = None`, does the block run? ::: No. `None` is falsy, so the block is skipped — a common trap when a variable is "unset".
What is the output of an `if` block whose condition is `True` but whose body is only a comment? ::: A `SyntaxError` (`expected an indented block`): a comment is not a statement, so the block is effectively empty. Use `pass` as a placeholder.
Does `else` after a lone `if` (no `elif`) still create a two-way split? ::: Yes. `if/else` is the simplest exhaustive split: exactly one of the two blocks runs, based on whether the single condition is true.
If a line after the `if` block is at the **same** indentation as the `if`, when does it run? ::: Always — it is **outside** the block, so it runs regardless of whether the condition was true or false.