Decisions with if, elif, and else
Use if, elif, and else to choose a message from a sample disk-usage number.
Based on the official Python documentation. Date checked: November 27, 2026. Script tested on Python 3.13.5.
Series: Python for small scripts, Part 3 of 10. Previous: Part 2: Variables, Types, and print. Next: Part 4: Loops with for and while.
TL;DR
if,elif, andelsechoose which block of code to run.- This script compares a sample percentage to two thresholds and prints one message.
- Python tests the conditions from the top and runs the first one that is true.
- Tested on Python 3.13.5. This is Part 3 of 10.
Before you start
- Python 3.11 or newer. This script was tested on Python 3.13.5. Check with
python3 --versionorpython --version. - Work in the
python-scriptsfolder from Part 1. - Run with
python3 disk.py. On Windows,python disk.pyis the usual form.
The finished script
Save this as disk.py in that folder:
used_percent = 72
if used_percent >= 90:
print("Alert: disk is nearly full")
elif used_percent >= 70:
print("Warning: disk is getting full")
else:
print("Disk usage is normal")
When you run it, you should see:
Warning: disk is getting full
Steps
- Save and run the script. Change into
python-scriptsand runpython3 disk.py. Why: you can see which branch ran before you change the number. You should see:Warning: disk is getting full. - Read the three branches.
>=means “greater than or equal to.” Python checksused_percent >= 90first.72is not 90 or more, so it moves toelif used_percent >= 70. That condition is true, so it prints the warning and skipselse. Why: only one of these blocks runs. The first true condition wins. - Change the sample number and run it again. Set
used_percent = 41, save, and run the script. You should see:Disk usage is normal. Set it to95and run it again. You should see:Alert: disk is nearly full. Why: these are numbers you typed, not a reading from a real disk, so you can try each branch safely. - Notice the colon and the indent. Each
if,elif, andelseline ends with:. The next line is indented. Why: the indent is how Python groups the lines that belong to that branch.
If it fails
SyntaxError: expected ':'. Theiforelifline is missing the colon at the end. Add:and run the script again. Python points at the end of the line:
File "missing_colon.py", line 2
if used_percent >= 90
^
SyntaxError: expected ':'
IndentationError: expected an indented block after 'if' statement on line 2. The line afterifis not indented. Indent it by four spaces (or one tab, and stay consistent) and run the script again.SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?. A condition such asif used_percent = 72:uses assignment (=). Use==to compare, and=only when you set a value.
Stay safe
used_percentis a sample number you typed. This script does not read a real disk.- Do not point a later script at a system path until you know what it will change.
- Use
==to compare values. Use=only to assign a value.
Next
Part 4: Loops with for and while
Sources
- Python docs, “More Control Flow Tools,”
ifstatements (docs.python.org/3/tutorial/controlflow.html#if-statements), Python 3.15.2 documentation, checked November 27, 2026. - Python docs, “Comparisons” (docs.python.org/3/library/stdtypes.html#comparisons), checked November 27, 2026.
Spot something wrong or outdated? Leave a comment below. Every comment is reviewed to keep these posts accurate.
By commenting, you agree to our Terms of use and confirm this is your own work. Short quotes are fine with a credit and link. Copied, unsafe, or abusive comments are removed. See something copied? Say so here.