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:

readingFirst true conditionOutput
17reading < 18low
18reading < 22ordinary
21reading < 22ordinary
22elsehigh

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?

Choose one

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.

Not attempted
Review

Not marked done.

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?

Choose one

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.

Not attempted
Review

Not marked done.

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"
Choose one

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".

Not attempted
Review

Not marked done.

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.

Pause and reflect

In your own words, note what you understood, what remains unclear, or what you want to revisit. The note stays with this lesson.

0 of 3 exercises marked done

Review

Not marked done.