Python conditionals — if, elif and else explained from scratch
Python conditionals if elif else are the first tool that gives your code intelligence. Until now your programs executed every line in order, top to bottom, making no decisions. With conditionals you can make Python execute certain instructions or others depending on whether a condition is met. It’s the leap from “program that calculates” to “program that decides”.
Table of Contents
What is a conditional?
A conditional is a structure that tells Python: “if this condition is met, do this, if not, do something else”. In real life we make conditional decisions constantly: if it’s raining, take an umbrella; if it’s not raining, don’t.
In Python that same logic is written like this:
raining = True
if raining:
print("Take an umbrella")
else:
print("No umbrella needed")
The if statement
The simplest form of a conditional is just the if. If the condition is True it executes the block, if it’s False it does nothing:
grade = 7.5
if grade >= 5:
print("Passed")
Two things you must always remember:
- The colon
:is mandatory — at the end of theifline. It’s the most common syntax error in first year, forgetting the colon. - Indentation is mandatory — the code that belongs to the
ifmust have 4 spaces of indentation. In Python indentation is not optional or decorative, it’s part of the syntax. If you don’t indent, Python gives an error.
# WRONG — missing indentation
if grade >= 5:
print("Passed") # IndentationError
# RIGHT
if grade >= 5:
print("Passed")
The if/else structure
When you want to do something different if the condition isn’t met, you add else:
grade = 4.0
if grade >= 5:
print("Passed")
else:
print("Failed")
The else doesn’t carry a condition, it simply executes when the if isn’t met. There can only be one else per if, and it always goes at the end.
The if/elif/else structure
When you need to check more than two possibilities you use elif, short for “else if”:
grade = 7.5
if grade >= 9:
print("Outstanding")
elif grade >= 7:
print("Merit")
elif grade >= 5:
print("Passed")
else:
print("Failed")
Python checks the conditions in order from top to bottom and executes the first block whose condition is True. Once it finds one, it ignores the rest. You can have as many elif as you need, but only one if at the start and one else at the end.
Nested conditionals
You can put an if inside another if. These are called nested conditionals and are useful when you need to check several related conditions:
age = 20
has_licence = True
if age >= 18:
if has_licence:
print("Can drive")
else:
print("Adult but no licence")
else:
print("Too young to drive")
Although they work fine, nested conditionals can become hard to read if there are many levels. In those cases it’s better to combine conditions with and and or:
# Cleaner using and
if age >= 18 and has_licence:
print("Can drive")
elif age >= 18:
print("Adult but no licence")
else:
print("Too young to drive")
The ternary operator — conditional in one line
Python allows writing a simple if/else in a single line. It’s called the ternary operator or conditional expression:
grade = 6.0 result = "Passed" if grade >= 5 else "Failed" print(result) # → Passed
It’s useful for simple cases where the full if/else would take four unnecessary lines. Don’t overuse it, if the condition is complex, write the full if/else so it’s more readable.
Most common mistakes with Python conditionals
Mistake 1 — Missing colon:
# WRONG
if grade >= 5
print("Passed")
# RIGHT
if grade >= 5:
print("Passed")
Mistake 2 — Using = instead of ==:
# WRONG — this is assignment, not comparison
if grade = 5:
print("Five")
# RIGHT
if grade == 5:
print("Five")
Use == to compare, never =.
Mistake 3 — Wrong indentation:
# WRONG — else is not aligned with if
if grade >= 5:
print("Passed")
else: # IndentationError
print("Failed")
# RIGHT
if grade >= 5:
print("Passed")
else:
print("Failed")
Mistake 4 — Wrong elif order (silent error):
# WRONG — first elif catches all passing grades
if grade >= 5:
print("Passed")
elif grade >= 7:
print("Merit") # never reached
elif grade >= 9:
print("Outstanding") # never reached
# RIGHT — from highest to lowest
if grade >= 9:
print("Outstanding")
elif grade >= 7:
print("Merit")
elif grade >= 5:
print("Passed")
else:
print("Failed")
This last mistake is especially treacherous because the program gives no error, it simply always shows “Passed” even when the grade is a 9. It’s a classic silent error. The fix is always to order conditions from the most restrictive to the least restrictive — highest value first.
Visualise with Python Tutor
Paste this code into pythontutor.com and step through it line by line:
grade = 8.5
if grade >= 9:
result = "Outstanding"
elif grade >= 7:
result = "Merit"
elif grade >= 5:
result = "Passed"
else:
result = "Failed"
print(result)
Try changing the value of grade and watch how Python jumps directly to the correct block, skipping all the others. Seeing the arrow skip from the if straight to the matching elif makes the logic much clearer than just reading about it.
Quick summary
# BASIC STRUCTURE
if condition: # : mandatory
code_block # 4 spaces mandatory
if condition:
code_if_true
else:
code_if_false
if condition1:
block1
elif condition2: # as many as needed
block2
else: # only one, always last
block_default
# TERNARY OPERATOR
result = "Passed" if grade >= 5 else "Failed"
# KEY RULES
# 1. : mandatory after if, elif and else
# 2. 4-space indentation mandatory
# 3. == to compare, = to assign — never mix them
# 4. elif order matters — from most to least restrictive
# 5. Once a condition matches, Python skips the rest
# COMMON ERRORS
if grade >= 5 # SyntaxError: expected ':'
if grade = 5: # SyntaxError: invalid syntax
# Wrong elif order → silent error — no message, wrong output
In the next article we practice Python conditionals with real programs, a data validator and the classic number guessing game.

4 Comments