Python Programming · Foundations
if, elif, and else
On this page 9 sections
In 30 seconds
An if statement chooses whether to run an indented block of code. Add elif clauses when there are more cases to test, and use else for the fallback when none of the earlier conditions succeeds. Python evaluates an if/elif chain from top to bottom and runs at most one selected branch. Put more specific cases before broader ones so an earlier branch does not catch them first.
Why this matters
Branching lets one program respond differently to different inputs or states: a score can receive a letter grade, or an order can receive a shipping rule. In coursework, it is a foundation for tracing program behavior and expressing requirements precisely. In later programs, clear branch order The top-to-bottom arrangement that determines which conditions get a chance to be tested. Full entry → makes policies easier to review and change. A correct condition is not enough if it appears after an earlier condition that already matches the same case.
The college version
A conditional statement is a sequence of clauses
Python places if, elif, and else in a compound statement. An if clause One keyword-led part of a compound statement, such as an if, elif, or else part. Full entry → begins with the keyword if, followed by an expression and a colon. The indented suite The indented block of statements controlled by a clause. Full entry → below it is the code associated with that clause. An elif clause has its own expression and suite; it can follow an if and can be repeated. An optional else clause has no expression. Its suite is the fallback when every preceding condition in that chain is false. Indentation is part of the statement structure, so code at the same indentation level as the if is outside the chain.
For a simple two-way choice, if plus else is often enough. For several mutually exclusive categories, an if/elif/else chain states the categories in their priority order. elif is Python's spelling for “else if”; it is not a separate nested statement that runs after a successful if. The official tutorial also notes that an if/elif sequence can fill the role that switch or case constructs serve in some other languages. Python also has match for some pattern-based choices, but that is a separate topic and is not needed to understand ordinary branch chains.
Selection proceeds from top to bottom
When Python reaches a chain, it evaluates the if expression first. If that expression is true, Python executes that suite and skips every elif and else in the same chain. If it is false, Python tries the first elif, then later elif clauses in order until one is true. If none succeeds and an else exists, Python runs the else suite. Thus a single chain selects zero or one suite: zero is possible when there is no else and every tested expression is false.
Order is therefore program logic, not formatting. Consider categories score >= 90, score >= 80, and score >= 70. They must appear in descending threshold order. If score >= 70 came first, a score of 93 would satisfy it immediately, and the later 90 and 80 branches would never be considered. This is a common source of quietly wrong results because each individual comparison can look reasonable when viewed alone. Use an else only when every remaining possibility truly belongs in the same fallback category; otherwise state another explicit condition.
Nested choices versus independent choices
A suite can contain another if statement, producing a nested decision. Nesting is useful when the second decision matters only after the first one has selected a context. For example, a program might first decide whether a user is a member and, only for members, choose among member-specific options. The inner statement has its own indentation and its own branch chain. Keeping nesting modest makes the path a reader must trace easier to see.
Do not confuse a chain with two independent if statements. Two separate if statements may both run: success in the first does not prevent Python from evaluating the second. An if/elif/else chain is for alternatives where one selected branch is intended. This distinction helps when translating a written requirement. Ask whether the program should choose one category, or whether it should carry out multiple applicable actions. When requirements change, name the cases in plain language first, then check whether their conditions overlap and which case should have priority. This small design step is often clearer than repairing a surprising result after the fact. The topic here is branch structure and order; the expressions themselves may use booleans or comparisons, but their detailed rules belong to the lessons on booleans and operators.
Planning a readable chain
Before writing the code, list a few representative inputs and the exact branch each input should choose. Include boundary values such as 70, 80, and 90 when thresholds are involved. This makes missing ranges and accidental overlaps visible before they become bugs.

Eli explains
The same idea, in plain words
Explain it like I’m 10
Imagine a school office sorting report cards. It looks at one card at a time. First it asks, “Is this score at least 90?” If yes, it puts the card in the A tray and stops asking questions for that card. If not, it asks the next question: “Is it at least 80?” It keeps going down the trays. If no labeled tray fits, the card goes in the last tray.
An if is the first question. Each elif is another question to ask only when the earlier answer was no. else is the last tray: it does not ask a question because it accepts what is left. The order matters. If the office asked “at least 70?” first, a 93 card would go into that tray too soon.
Picture it like this
A top-to-bottom set of labeled sorting trays for one card.
Where the picture stops working
Programs evaluate expressions, not people or physical cards, and an else branch can perform any code rather than merely sorting an item.
Worked example
This function classifies a numeric score. Python tests the thresholds from highest to lowest, so the first matching threshold chooses the result.
def label(score):
if score >= 90:
return "A"
elif score >= 80:
return "B"
elif score >= 70:
return "C"
else:
return "Needs work"
for score in (93, 84, 71, 62):
print(score, label(score))Running it with Python 3 prints 93 A, 84 B, 71 C, and 62 Needs work. For 84, the 90 test fails, the 80 test succeeds, and the function returns B; later clauses are skipped. Reversing the 70 and 90 tests would misclassify 93 because the broad 70 condition would win first.
Key takeaway
An if/elif/else chain tests alternatives in written order and selects at most one branch. Put overlapping cases in the correct priority order, and reserve else for the true fallback.
Quick check
3 questions here, of 5 in this lesson’s practice set. Answers stay hidden until you check.
What is the role of an
A chain begins with if score >= 70 and later has elif score >= 90. What happens for score = 93?
Which arrangement correctly expresses mutually exclusive score bands?
Study tools & related lessonsYou’ll learn to · Common mistakes · Easily confused · Key vocabulary · Related
You’ll learn to
- Define an
ifclause, anelifclause, and anelseclause. - Explain how Python selects one branch in an if/elif/else chain.
- Apply ordered conditions to a small classification problem.
- Distinguish an
elsefallback from another condition that must be tested. - Analyze how branch order can make a later case unreachable.
Common mistakes
Putting a broad threshold before a stricter threshold.
Order overlapping ranges from most specific or highest threshold to broadest.
Expecting a later elif to run after a successful if.
Use a separate if only when more than one action may legitimately run.
Giving else a condition.
Put a condition on elif; use else only for the remaining case.
Losing the suite indentation.
Indent every statement controlled by a clause consistently.
Easily confused
elif vs. else
An elif tests a new expression after earlier failures; else is the untested fallback.
if/elif/else chain vs. separate if statements
A chain selects at most one suite, while separate if statements can each execute.
Key vocabulary
- conditional statement
- A statement that chooses whether to execute a suite based on tested conditions.
- clause
- One keyword-led part of a compound statement, such as an if, elif, or else part.
- suite
- The indented block of statements controlled by a clause.
- if clause
- The first clause in a conditional chain; its expression is tested first.
- elif clause
- An optional alternative clause whose expression is tested only after earlier clauses fail.
- else clause
- An optional fallback clause that runs when no earlier condition in its chain succeeds.
- branch order
- The top-to-bottom arrangement that determines which conditions get a chance to be tested.
Sources & references
- 8. Compound statements - The Python Language Reference (while, for, break, continue) — Python Software Foundation
- The Python Tutorial — 4. More Control Flow Tools (if Statements) — Python Software Foundation
EliExplains lessons are original prose written from the open, credible references above. See Copyright & Licensing.
Researched 2026-08-19
Educational content only. It is not medical, legal or professional advice. Found an error? Tell us.

