Choose with if, elif, and else
Use an ordered branch chain to classify one reading as low, ordinary, or high. Trace indentation, first-match behavior, and exact threshold values.
A boolean tells us the answer to a question. A conditional lets the program use that answer to choose work. Suppose a temperature reading can be low, ordinary, or high:
The output is:
high
Choose One Indented Block
if begins by asking its condition. The indented lines below it are its
block: work Python performs only when that condition is true. If the first
condition is false, elif asks another question. else supplies the remaining
case when every earlier question was false.
Indentation is part of Python's structure. The four spaces before each print
show which instruction belongs to the branch above it. Keep the indentation
consistent; it tells both Python and the reader where the selected work begins
and ends.
For this classification, Python tests the branches from top to bottom and uses exactly one block. These boundary cases show why the order matters:
reading | First true condition | Output |
|---|---|---|
| 17 | reading < 18 | low |
| 18 | reading < 22 | ordinary |
| 21 | reading < 22 | ordinary |
| 22 | else | high |
At 18, the first question is false, so Python reaches elif. At 22, both
written comparisons are false, so Python reaches else.
Q1. Follow the selected branch
What does this program display?
Select one choice, then check.
HintTest from the top
18 < 18 is false. The next condition still has a chance to run.
SolutionThe elif block runs
The first comparison fails at the boundary. 18 < 22 is true, so Python
selects the indented elif block and prints ordinary.
Put Overlapping Cases in the Right Order
The branch conditions may describe overlapping groups. Python does not search for the most specific description. It stops at the first true branch.
This ordering classifies readings of 22 or more as high:
The elif condition also accepts 22, 23, and larger values. It is safe only
because the earlier if has already selected those high values. Put the high
test first, then the broader ordinary test.
Q2. Order the high threshold
Which order correctly prints high for a reading of 24?
Select one choice, then check.
HintAsk which condition 24 reaches first
Both threshold comparisons are true for 24. The first true one decides.
SolutionTest high first
reading >= 22 is the more specific condition. Testing it first prevents 24
from being classified by the broader reading >= 18 branch.
A Short Form for Two Outcomes
When a program needs one of two values, Python also has a conditional expression. First write and understand the full branch:
The equivalent short form is:
label = "high" if reading >= 22 else "ordinary"
Read it from the middle: if the condition is true, label receives "high";
otherwise it receives "ordinary". The full branch remains clearer when there
are three outcomes or several instructions in a block.
Q3. Read the short conditional
With reading = 21, what value does this instruction assign to label?
label = "high" if reading >= 22 else "ordinary"
Select one choice, then check.
HintCheck the middle condition
Is 21 greater than or equal to 22?
Solutionlabel receives ordinary
The condition is false for 21. Python therefore uses the value after else,
so label refers to "ordinary".
An if/elif/else chain tests conditions in order and selects one indented
block. Next we will combine simple questions when one comparison is not enough
to describe the intended case.