Reading preferences
Concept

Decisions with if

Use conditions and indentation to choose which instructions run.

python, conditions, if, elif, else, indentation

Choose a path

If rain is falling, take an umbrella; otherwise, leave it at home. A program can also choose different instructions based on a condition, a question whose result determines which path to follow.

For a number of library books, decide whether none, one, or several are available. In an if / elif / else chain, Python selects the first true condition; else handles the remaining cases.

books == 0? ─ yes → none
     │ no
books == 1? ─ yes → one
     │ no
     └────────────→ several

Python illustration

books = 2
if books == 0:
    message = "none"
elif books == 1:
    message = "one"
else:
    message = "several"
print(message)

Prepared output — not live execution:

several

The colon begins each block. Four spaces place message = ... inside a branch. The final print is not indented, so it runs after the selected branch. Consistent indentation matters to Python, not just appearance.

See the reasoning

Follow the example one step at a time

Follow the lesson's if / elif / else chain when books is 2. This is a prepared teaching demonstration, not live code execution.

Start

books → 2

We assume books is a non-negative whole number.

Check boundaries

This example assumes a non-negative whole number of books; a negative input should be rejected or handled separately, not silently described as several. Test 0, 1, and 2, especially when using <, <=, > or >=. Use == for equality; a single = assigns a value rather than testing equality.

Reflect on the concept

Four fixed questions to reinforce the reasoning. Answers are not saved or scored.

1. When does an if block run?
2. When is an elif condition checked?
3. What shows that a line belongs inside the if block?
4. Which expression checks whether count equals 2?

References and further reading