Python Tutorial / Choose Execution Paths with if, elif and else
An if–elif–else chain chooses an execution path by checking conditions from top to bottom. Only the first matching branch runs, so design the ordering and boundaries before writing the conditions.
Use indentation to define each branch
A condition ends with a colon and its statements are indented consistently, conventionally by four spaces. Mixing tabs and spaces can make visual alignment misleading. else has no condition.
Two separate if statements can both run. An if–elif chain selects one branch. Decide whether your rules describe mutually exclusive categories or independent checks that should all be evaluated.
For the relevant APIs and platform requirements, see Python: controlflow.
Condition combinations and boundary checks build on the rules demonstrated in Compare Values and Combine Boolean Conditions and Practice Conditions and Loops with a Score Summary.
Run a complete classification
Save the complete code below as classify.py in a separate practice folder, using UTF-8. Run python classify.py in a terminal with your project virtual environment selected. Outside a virtual environment on macOS or Ubuntu, use python3 when that is your interpreter command.
amount = 8000
if amount < 0:
label = "invalid"
elif amount == 0:
label = "free"
elif amount <= 10000:
label = "small"
else:
label = "large"
print(label)
Expected output:
small
Define the ranges and boundaries
The example separates negative amounts, zero, 1 through 10000, and amounts above 10000. Earlier conditions eliminate cases before later ones are considered. A broad condition placed too early can make a later branch unreachable.
| Input | Expected label |
|---|---|
| -1 | invalid |
| 0 | free |
| 1 or 10000 | small |
| 10001 | large |
This classification assumes a numeric amount. Converting user text with int() and handling a failed conversion are separate tasks from deciding the amount category. See the input and output example for that boundary.
If result is assigned only in the true branch, using it after a false branch can reference an undefined name. Set a default before the branch or assign in both if and else. Inside a function, a name assigned in the function is normally local, so a path that reads it first can raise UnboundLocalError. Check every input path.
Check every path and keep values defined
Any variable used after the chain must receive a value on every possible path. Assigning label in only some branches can produce NameError for another input. Favor a clear account of where a value is assigned over making the code as short as possible.
- Substitute every input from the table and verify its branch.
- Change <= to < and observe the new classification of 10000, then restore it.
- Consider two independent warnings and decide whether separate if statements better express the rules.
If a branch appears to do nothing, inspect its condition and earlier matches before adding more nesting. Boundary examples are usually more informative than repeatedly testing a typical middle value.




