Aral Code

Decisions & Loops Lesson 11 of 23

Python if, elif and else

Make decisions in Python with if, elif and else. Build a DepEd grade descriptor, a voting eligibility checker and learn the one-line conditional expression.

Programs become useful when they can make choices: show "PASSED" or "FAILED", give a discount or not, allow login or deny it. In Python, decisions use if, elif (short for "else if") and else.

if

grade = 88
if grade >= 75:
    print("PASSED 🎉")
print("End of program")

Output

PASSED 🎉
End of program

Remember the two rules from the Syntax lesson: the if line ends with a colon, and the code inside is indented 4 spaces.

if…else

grade = int(input("Grade: "))
if grade >= 75:
    print("PASSED 🎉")
else:
    print("FAILED. Kaya mo 'yan next time! 💪")

Output

Grade: 70
FAILED. Kaya mo 'yan next time! 💪

elif: more than two choices

Python checks each condition from top to bottom and runs only the first block whose condition is true:

grade = int(input("Final grade: "))

if grade >= 90:
    descriptor = "Outstanding"
elif grade >= 85:
    descriptor = "Very Satisfactory"
elif grade >= 80:
    descriptor = "Satisfactory"
elif grade >= 75:
    descriptor = "Fairly Satisfactory"
else:
    descriptor = "Did Not Meet Expectations"

print(f"{grade}: {descriptor}")

Output

Final grade: 87
87: Very Satisfactory

Combining conditions

age = int(input("Age: "))
registered = input("Registered voter? (yes/no) ").strip().lower() == "yes"

if age >= 18 and registered:
    print("You can vote. 🗳️")
elif age >= 18:
    print("Register first at COMELEC.")
else:
    print(f"Wait {18 - age} more year(s).")

Output

Age: 16
Registered voter? (yes/no) no
Wait 2 more year(s).

Nested if

An if can go inside another if. Each level adds 4 more spaces:

has_ticket = True
age = 10

if has_ticket:
    if age < 12:
        print("Child ticket: please sit with an adult.")
    else:
        print("Enjoy the movie!")
else:
    print("Please buy a ticket first.")

Output

Child ticket: please sit with an adult.

Nesting more than two levels deep gets hard to read. Often and or elif gives a flatter, clearer version.

One-line conditional expression

For a quick either-or value, write it on one line:

grade = 79
status = "PASSED" if grade >= 75 else "FAILED"
print(status)

count = 1
print(f"{count} item" if count == 1 else f"{count} items")

Output

PASSED
1 item

pass: an empty block

Python doesn't allow an empty block. If you want to fill it in later, write pass:

if grade >= 90:
    pass   # TODO: print a certificate

match: choosing by exact value

Python 3.10 added match, which is like switch in other languages:

day = "Fri"
match day:
    case "Mon":
        print("Flag ceremony 🇵🇭")
    case "Fri":
        print("Coding club 💻")
    case "Sat" | "Sun":
        print("Weekend!")
    case _:
        print("Regular class day")

Output

Coding club 💻

case _ matches anything else, like else. For ranges like grades, keep using if/elif.

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. What does elif mean?
2. How many blocks run in an if/elif/else chain?
3. What's missing in if grade >= 75?
4. What does "yes" if 5 > 9 else "no" give?

By Sir Reyel · Last updated 2026-12-04