Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How does Python choose which `if` or `elif` branch to run?

Quick answer

Python checks an if condition, then any elif conditions in order. As soon as one is true, Python runs that branch and skips the rest of the chain—even if a later condition would also be true. Put the most specific or narrow condition first when conditions overlap. For example, check score >= 90 before score >= 50 if you want high scores labeled “Excellent.” [S1]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I have a few elif conditions that seem to overlap. How does Python decide which one to run?

Geez

Python checks the conditions from top to bottom. It runs the first branch whose condition is true, then skips the remaining branches in that if/elif/else chain. [S1]

So the order matters whenever more than one condition could be true. Here, a score of 92 satisfies both score >= 50 and score >= 90, but the broader test comes first:

score = 92

if score >= 50:
    print("Pass")
elif score >= 90:
    print("Excellent")
else:
    print("Below pass")

This prints Pass. Python reaches that first condition, finds it true, and never checks the elif.

Follow-up question

Why doesn’t Python continue and choose Excellent too? The score meets that condition as well.

Geez

An if/elif chain selects at most one branch: the first true one. It does not compare all the matching conditions and choose the narrowest, highest, or most descriptive result. Once score >= 50 is true, the chain is finished for that run. [S1]

The key is to arrange the conditions so the first match is the category you intend. If you want a value to receive a more specific label, check the condition for that label before a broader condition that would also match it.

Follow-up question

How should I reorder the score checks so 92 is labeled Excellent, while 70 is Pass?

Geez

Put the narrower, higher threshold first, then the broader passing threshold:

score = 92

if score >= 90:
    print("Excellent")
elif score >= 50:
    print("Pass")
else:
    print("Below pass")

With score = 92, Python prints Excellent. If you change it to score = 70, the first test is false and the second is true, so it prints Pass. A score below 50 reaches else. This works because a score of at least 90 also meets the 50 threshold, but gets handled before Python reaches that broader condition.

Follow-up question

Is there a quick way to check whether my own if/elif order is causing the problem?

Geez

Try a value that you believe should match a later branch, then test the earlier conditions against that same value. If an earlier condition is already true, it blocks the later branch. For the example, try 92: it meets both thresholds, so score >= 50 must not be placed ahead of score >= 90 if 92 should be labeled Excellent.

A practical review is: list the cases from most specific to broadest; try a value at each boundary and one on either side; and confirm the else message is appropriate for anything not matched. Python’s branch selection does not tell you whether your thresholds or labels match your intended rules—you must check those choices against your own requirements.

A practical checklist

  • Identify conditions that can both be true for the same input.

  • Move the narrower or higher-threshold condition above the broader one.

  • Test a value that should match each elif branch.

  • Test values at and just below each threshold.

  • Confirm that else describes the remaining unmatched cases.

A few more useful answers

Can more than one `elif` branch run in a single chain?

No. Python runs the first true branch in the chain and skips the rest. [S1]

Should I always put the largest number first?

Not automatically. Put conditions in the order that gives the intended result; for overlapping numeric ranges, a more specific or higher-threshold test often needs to come before a broader one.

Sources

References used for this guide.

  1. [S1] Python Language Reference: if statements
    docs.python.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work