Course outline · 0% complete

0/27 lessons0%

Course overview →

elif, and, or

lesson 4-3 · ~10 min · 14/27

More than two branches

Real rules rarely come in pairs. Letter grades have five bands, shipping costs have weight tiers, and tax has brackets. Programs need a clean way to pick one outcome out of several, and nesting separate if/else statements inside each other gets unreadable fast, so Python provides a flat chain instead.

elif, short for "else if", chains extra conditions. Python checks them top to bottom and runs only the first one that is True:

score = 72
if score >= 90:
    print("A")
elif score >= 80:
    print("B")
elif score >= 70:
    print("C")
else:
    print("F")

With score = 72: is 72 ≥ 90? No. Is 72 ≥ 80? No. Is 72 ≥ 70? Yes, so print C and skip the rest.

Order matters. Put the strictest condition first, or an earlier looser one will steal the match. Reversing this chain to check >= 70 first would print C for every passing score, including a 95.

score = 72 if 72 >= 90 elif 72 >= 80 elif 72 >= 70 else, never reached False False print("C") chain stops at the first True
An if/elif/else chain is checked top to bottom and stops at the first True. A score of 72 fails the first two tests, matches the third, and never reaches the else.

The chain picking one branch

score is 72, so the third condition is the first one that holds.

score = 72
if score >= 90:
    print("A")
elif score >= 80:
    print("B")
elif score >= 70:
    print("C")
else:
    print("F")

Output

C

Exactly one branch runs, the first whose condition is True. Walking other values through the same chain: 91 prints A, 80 prints B on the second check, and 12 falls through every condition to the else and prints F.

The 80 case is the one to watch, because >= 80 accepts it while >= 90 rejects it. Every band boundary in a chain like this belongs to the branch above it, which is exactly the kind of detail a grading system gets complaints about.

Combining conditions

Three words let you build compound conditions from the comparisons in lesson 4-1:

  • and is True only when both sides are True
  • or is True when at least one side is True
  • not flips True to False and back
age = 25
has_ticket = True
if age >= 18 and has_ticket:
    print("Welcome in!")
else:
    print("Sorry, no entry.")

Note that has_ticket holds a boolean directly, so it can be used as a condition all by itself. There is no need for has_ticket == True, because the variable already is True or False, and writing the comparison just adds a step that answers a question you already have the answer to.

That is also a hint about naming. Boolean variables read best when their names sound like yes-or-no questions, which is why has_ticket and is_weekend are the conventional shapes.

Evaluating an or

With is_weekend = False and is_holiday = True, the expression (is_weekend or is_holiday) evaluates to True.

or needs only one side to be True, and is_holiday supplies it. The False on the left does not matter.

and is the strict one that requires both sides, and swapping the operator here would give False.

A reliable way to keep them straight is to read them as everyday English promises. "Free entry on a weekend or a holiday" lets you in on either, and "bring your ID and your ticket" requires both.

A three-band temperature chain

temperature is 5, which fails both conditions and lands in the else.

temperature = 5
if temperature >= 30:
    print("hot")
elif temperature >= 15:
    print("warm")
else:
    print("cold")

Output

cold

Reading the pieces

  • Three branches: the if, one elif, and a final else with no condition at all. The else is the catch-all, so the chain always prints exactly one thing.
  • The >= 30 check has to come first. If >= 15 came first, a temperature of 35 would satisfy it and wrongly print warm, because the chain stops at the first match.
  • With temperature = 5 both conditions are False, so the else branch prints cold. That branch is what stops the program from silently printing nothing for cold days.