Python if-else and elif Explained
Learn how Python chooses between different code paths using if, elif, and else. This page helps beginners read and write basic conditional logic with clear examples.
Quick example
age = 18
if age < 13:
print("child")
elif age < 18:
print("teen")
else:
print("adult")
Use if for the first condition, elif for extra conditions, and else for the fallback case.
Output:
adult
What if, elif, and else do
Python uses conditional statements to decide which code should run.
ifchecks a condition firstelifmeans “else if” and checks another condition if earlier ones were falseelseruns when no earlier condition matched- Python runs only the first matching branch in one
if-elif-elsechain
A condition is something that becomes True or False. If you are new to that idea, see Python booleans explained: true and false.
Here is a small example:
temperature = 30
if temperature > 35:
print("Very hot")
elif temperature > 25:
print("Warm")
else:
print("Cool")
Python checks from top to bottom:
- Is
temperature > 35? - If not, is
temperature > 25? - If not, run
else
Output:
Warm
Basic if-else structure
Use if when you want code to run only when a condition is True.
Add else when you want a fallback action.
Basic structure:
if condition:
print("This runs when the condition is True")
else:
print("This runs when the condition is False")
Important rules:
- The condition must end with a colon
: - The code inside each branch must be indented
- Indentation matters in Python
Example:
is_raining = True
if is_raining:
print("Take an umbrella")
else:
print("No umbrella needed")
Output:
Take an umbrella
If you want a more focused introduction, see Python if statements explained. If your code fails because of spacing, read Python indentation rules and why they matter.
How elif works
Use elif when there are multiple possible cases.
Python checks conditions from top to bottom. The first True condition wins, and the rest are skipped.
Example:
score = 82
if score >= 90:
print("Grade A")
elif score >= 80:
print("Grade B")
elif score >= 70:
print("Grade C")
else:
print("Grade D or below")
Output:
Grade B
Why order matters
Look at this example:
number = 5
if number > 0:
print("Positive")
elif number > 3:
print("Greater than 3")
else:
print("Other")
Output:
Positive
Even though number > 3 is also true, Python never gets that far. The first matching branch already ran.
When needed, write more specific conditions before broader ones:
number = 5
if number > 3:
print("Greater than 3")
elif number > 0:
print("Positive")
else:
print("Other")
This version gives a different result because the order changed.
Reading conditions in order
A good way to understand an if-elif-else chain is to read it step by step:
- Start with the first
ifcondition - If it is
False, move to the nextelif - Keep checking until one is
True - If none are
True,elseruns if it exists
Example:
age = 16
if age < 13:
print("child")
elif age < 18:
print("teen")
else:
print("adult")
Python reads it like this:
- Is
age < 13? No - Is
age < 18? Yes - Run
print("teen") - Stop checking the chain
Output:
teen
This is why if-elif-else is different from writing several separate if statements.
Common comparison operators used in conditions
You often compare values inside conditions.
Common comparison operators:
==means equal to!=means not equal to>means greater than<means less than>=means greater than or equal to<=means less than or equal to
Example:
x = 10
if x == 10:
print("x is 10")
if x != 5:
print("x is not 5")
if x >= 8:
print("x is at least 8")
Output:
x is 10
x is not 5
x is at least 8
Be careful with = and ==.
=assigns a value==compares two values
This is a very common beginner mistake.
Common beginner mistakes
Here are some problems beginners often run into with if, elif, and else.
Using = instead of ==
This is wrong:
age = 18
if age = 18:
print("adult")
Use == for comparison:
age = 18
if age == 18:
print("adult")
Forgetting the colon
Each if, elif, and else line must end with a colon.
Correct:
value = 3
if value > 0:
print("positive")
else:
print("not positive")
If you forget the colon, Python raises a syntax error. See SyntaxError: missing colon.
Incorrect indentation
The code inside each branch must be indented.
Correct:
name = "Sam"
if name == "Sam":
print("Hello, Sam")
Bad indentation can cause an error such as IndentationError: expected an indented block.
Writing overlapping conditions in the wrong order
This example has the wrong order:
age = 12
if age < 18:
print("Under 18")
elif age < 13:
print("Child")
else:
print("Adult")
Output:
Under 18
The elif age < 13 branch never runs, because age < 18 is already true first.
A better version is:
age = 12
if age < 13:
print("Child")
elif age < 18:
print("Under 18")
else:
print("Adult")
Expecting more than one branch to run
In one if-elif-else chain, only one branch runs.
If you need multiple checks to run, you may need separate if statements instead of one chain.
When to use nested conditions
Nested conditions are if statements inside another if block.
Use them when a second check depends on a first check.
Example:
age = 20
has_id = True
if age >= 18:
if has_id:
print("Entry allowed")
else:
print("ID required")
else:
print("Too young")
Output:
Entry allowed
This works, but try to keep nesting shallow so your code stays readable.
If your logic starts to feel confusing:
- simplify the conditions
- check the order carefully
- use clear variable names
- print values while testing
Common causes of confusion
These are common reasons if-elif-else code does not behave as expected:
- Confusing one
if-elif-elsechain with separateifstatements - Placing a broad condition before a specific one
- Forgetting that
elsehas no condition - Mixing tabs and spaces in indentation
- Using a comparison that does not match the intended logic
These quick checks can help while debugging:
print(condition)
print(age)
print("checking first condition")
help(bool)
For example:
age = 16
print(age)
print(age < 13)
print(age < 18)
if age < 13:
print("child")
elif age < 18:
print("teen")
else:
print("adult")
Output:
16
False
True
teen
Printing the values and condition results helps you see why a branch did or did not run.
FAQ
What is the difference between if and elif?
if starts the condition chain. elif adds another condition that is checked only if earlier ones were False.
Do I have to use else?
No. else is optional. Use it when you want a default action.
Can I have more than one elif?
Yes. You can use as many elif branches as needed.
Can more than one branch run?
No. In one if-elif-else chain, Python runs only the first branch whose condition is True.
What happens if all conditions are False and there is no else?
Nothing in that chain runs, and Python continues to the next line after it.
See also
- Python if statements explained
- Python booleans explained: true and false
- Python indentation rules and why they matter
- What is an if statement in Python?
Next, learn how conditions work with loops or practice by writing a small input-based program that chooses between different outputs.