elif / else
Updated March 26, 2026 · Keywords
control-flow conditionals exceptions loops
elif — Chain Conditional Tests
elif is short for “else if”. It extends an if statement to test additional conditions in sequence. If the if condition is False, Python evaluates each elif in order until one is True. As soon as a match is found, its body runs and all remaining branches are skipped.
temperature = 72
if temperature > 85:
print("hot")
elif temperature > 65:
print("warm")
elif temperature > 50:
print("cool")
else:
print("cold")
# warm
```python
`elif` cannot stand alone — it must always follow an `if` statement. There is no limit to how many `elif` clauses you can chain. An `else` at the end is optional.
### Nested `elif`
An `elif` only ever chains to the immediately preceding `if`. Inner `if`/`elif`/`else` blocks are independent:
```python
x = 10
if x > 0:
if x > 100:
print("big positive")
elif x > 50:
print("medium positive")
else:
print("small positive")
elif x < 0:
print("negative")
# small positive
```python
---
## `else` on `if` / `elif` Chains
The `else` block executes only when **every** condition in the `if`/`elif` chain evaluated to `False`. It appears at the end of the chain, after all `elif` clauses:
```python
score = 55
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
elif score >= 70:
grade = "C"
elif score >= 60:
grade = "D"
else:
grade = "F"
print(grade) # F
```python
---
## `else` on Loops
Python is unique among mainstream languages: the `else` clause on a `for` or `while` loop runs when the loop finishes **without executing `break`**.
```python
def is_prime(n):
if n < 2:
return False
for i in range(2, int(n ** 0.5) + 1):
if n % i == 0:
return False
return True
for n in range(2, 10):
if is_prime(n):
print(f"{n} is prime")
else:
print(f"{n} is not prime")
# 2 is prime
# 3 is prime
# 4 is not prime
# 5 is prime
# 6 is not prime
# 7 is prime
# 8 is not prime
# 9 is not prime
```python
This pattern eliminates the need for a sentinel flag variable when searching:
```python
# Without loop else — flag variable
found = False
for item in items:
if item == target:
found = True
break
if not found:
handle_missing()
# With loop else — cleaner
for item in items:
if item == target:
handle_found(item)
break
else:
handle_missing()
```python
If you do not use `break` in a loop, do not use `else` — it will always execute and mislead the reader.
---
## `else` on `try` / `except`
The `else` clause on a `try` block runs **only if no exception was raised** in the `try` body. It sits between the `except` clauses and the optional `finally`:
```python
def parse_port(value):
try:
port = int(value)
except ValueError:
return "invalid"
else:
if 1 <= port <= 65535:
return f"valid port {port}"
else:
return "port out of range"
finally:
print("parsing complete")
print(parse_port("8080")) # valid port 8080
print(parse_port("hello")) # invalid
print(parse_port("99999")) # port out of range
# parsing complete (printed three times)
```python
Use `else` to separate the "happy path" logic from exception handling. Keep `except` focused on errors and `else` focused on success.
---
## Conditional Expression (Ternary)
Python's conditional expression is an `if`/`else` **expression**, not a statement block. It evaluates to one of two values:
```python
x = 5
result = "positive" if x > 0 else "zero or negative"
print(result) # positive
```python
Short-circuit evaluation means only the chosen branch runs:
```python
value = default if (cond := get_value()) is None else cond
```python
Chained ternary expressions are valid but degrade quickly in readability:
```python
label = "high" if x > 100 else "medium" if x > 50 else "low"
```python
For more than two branches, prefer a regular `if`/`elif`/`else` block — it is clearer and easier to maintain.
---
## Common Mistakes
Using multiple `if` statements instead of `elif` causes every condition to be evaluated, even after a match:
```python
# WRONG — all branches tested even after a match
if score >= 90:
grade = "A"
if score >= 80:
grade = "B" # overwrites A if score >= 90
# CORRECT — elif stops at the first True condition
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
```python
---
## See Also
- [The `if` keyword](/reference/keywords/if-keyword/) — parent conditional statement
- [The `try` keyword](/reference/keywords/try-keyword/) — exception handling with `else` clause
- [The `for` keyword](/reference/keywords/for-keyword/) — loop with `else` clause