If, Elif & Else
Learn how to make decisions in your Python code using if, elif, and else.
If, Elif & Else
Your code does not always run top to bottom in a straight line. Sometimes you need to make a decision — do this if something is true, do something else if it is not. That is exactly what if, elif, and else are for.
if — the basic decision
The simplest form. If the condition is true, run the block. If it is not, skip it.
age = 20
if age >= 18:
print("You are an adult.")Output:
You are an adult.If age was 15, nothing would print — the condition is false, so Python skips the whole block.
The structure is always the same:
if condition:
code to run if condition is TrueThe colon : at the end of the if line is required. The code inside must be indented — that is how Python knows it belongs to the if.
Indentation is not optional in Python. The standard is 4 spaces. If your indentation is inconsistent, Python raises an IndentationError.
else — the fallback
else runs when the if condition is false. You are saying — if that was not true, then do this instead.
age = 15
if age >= 18:
print("You are an adult.")
else:
print("You are a minor.")Output:
You are a minor.One of the two blocks always runs — never both, never neither.
elif — checking more conditions
elif stands for "else if". It lets you check a second condition if the first one was false, a third if the second was false, and so on.
score = 72
if score >= 90:
print("Grade: A")
elif score >= 80:
print("Grade: B")
elif score >= 70:
print("Grade: C")
else:
print("Grade: F")Output:
Grade: CPython checks each condition from top to bottom. The moment one is true, it runs that block and stops. The rest are skipped — even if they would also be true.
score = 95
if score >= 70:
print("Grade: C") # this is true — runs here, stops
elif score >= 80:
print("Grade: B") # never checked
elif score >= 90:
print("Grade: A") # never checkedOutput:
Grade: CThis is wrong logic — always order your conditions from most specific to least specific, or highest to lowest when working with ranges.
How Python walks through conditions
Only one block ever runs. Python finds the first true condition, runs it, and jumps to the end.
Nested if
You can put an if inside another if. This is called nesting.
age = 20
has_ticket = True
if age >= 18:
if has_ticket:
print("Welcome in!")
else:
print("You need a ticket.")
else:
print("You must be 18 or older.")Output:
Welcome in!Nesting works, but do not go too deep — more than two levels becomes hard to read. When that happens, combine conditions with and instead:
if age >= 18 and has_ticket:
print("Welcome in!")
else:
print("Entry denied.")Same result, much cleaner.
Combining conditions
Use and, or, and not to build more powerful conditions:
# and — both must be true
username = "ali"
password = "1234"
if username == "ali" and password == "1234":
print("Logged in!")
else:
print("Wrong credentials.")# or — at least one must be true
day = "Saturday"
if day == "Saturday" or day == "Sunday":
print("It's the weekend!")
else:
print("It's a weekday.")# not — flips the condition
is_banned = False
if not is_banned:
print("Access granted.")Truthy and falsy in conditions
You do not always need == True or == False. Python evaluates any value as true or false directly:
name = ""
if name:
print(f"Hello, {name}!")
else:
print("Please enter your name.")
# Please enter your name.items = [1, 2, 3]
if items:
print(f"You have {len(items)} items.")
else:
print("Your list is empty.")
# You have 3 items.Empty strings, empty lists, 0, and None are all falsy. Everything else is truthy. Python checks this automatically inside if.
Ternary — one line if/else
For simple cases, Python lets you write an if/else on a single line:
age = 20
status = "adult" if age >= 18 else "minor"
print(status) # adultThe structure is:
value_if_true if condition else value_if_falseUse this only when it is genuinely simple. If the logic is complex, stick to the normal multi-line style — readability matters more than saving a line.
A real example
temperature = float(input("Enter the temperature (°C): "))
if temperature >= 35:
print("It's very hot. Stay hydrated.")
elif temperature >= 25:
print("It's warm. Nice weather.")
elif temperature >= 15:
print("It's mild. A light jacket is fine.")
elif temperature >= 5:
print("It's cold. Wear a coat.")
else:
print("It's freezing. Stay inside!")Output:
Enter the temperature (°C): 28
It's warm. Nice weather.Summary
| Keyword | Purpose |
|---|---|
if | Run a block if a condition is true |
elif | Check another condition if the previous was false |
else | Run a block if all conditions above were false |
- Conditions are checked top to bottom
- Only the first true block runs — the rest are skipped
elseis optional —elifis optional —ifis always required- Use
and,or,notto combine conditions - Keep nesting shallow — combine with
and/orwhen you can