📚 Reference guide

Decisions with if, elif, and else

Use if, elif, and else to choose a message from a sample disk-usage number.

Series: Python for small scripts

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

Before you start

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

  1. Save and run the script. Change into python-scripts and run python3 disk.py. Why: you can see which branch ran before you change the number. You should see: Warning: disk is getting full.
  2. Read the three branches. >= means “greater than or equal to.” Python checks used_percent >= 90 first. 72 is not 90 or more, so it moves to elif used_percent >= 70. That condition is true, so it prints the warning and skips else. Why: only one of these blocks runs. The first true condition wins.
  3. 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 to 95 and 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.
  4. Notice the colon and the indent. Each if, elif, and else line ends with :. The next line is indented. Why: the indent is how Python groups the lines that belong to that branch.

If it fails

  File "missing_colon.py", line 2
    if used_percent >= 90
                         ^
SyntaxError: expected ':'

Stay safe

Next

Part 4: Loops with for and while

Sources

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.