Python If Statements: If, Else, and Elif
Table of Contents
Every useful program needs to make decisions. Python’s if statement is how you tell your code to do one thing or another depending on some condition — and once you understand if, elif, and else, you can handle just about any branching logic.
All the content from our Boot.dev courses are available for free here on the blog. This one is the “Comparisons” chapter of Learn to Code in Python. If you want to try the far more immersive version of the course, do check it out!
What Are Comparison Operators in Python?
Before you write your first if statement, you need to know how to compare values. Boolean logic is the name for these kinds of comparison operations that always result in True or False.
Python gives you six comparison operators:
| Operator | Meaning |
|---|---|
< | less than |
> | greater than |
<= | less than or equal to |
>= | greater than or equal to |
== | equal to |
!= | not equal to |
5 < 6 # True
5 > 6 # False
5 >= 6 # False
5 <= 6 # True
5 == 6 # False
5 != 6 # True
When a comparison happens, the result is just a boolean value that you can store in a variable. These two lines do the same thing:
is_bigger = 5 > 4
is_bigger = True
Because 5 is greater than 4, is_bigger is assigned the value of True. You can store comparisons in variables just like any other value.
How Do If Statements Work in Python?
An if statement lets you run code only when a condition is True:
if CONDITION:
# this code runs only when CONDITION is True
For example:
def show_status(boss_health):
if boss_health > 0:
print("Ganondorf is alive!")
return
print("Ganondorf is unalive!")
If boss_health is greater than 0, the function prints "Ganondorf is alive!" and returns early. Otherwise, it prints "Ganondorf is unalive!".
Without that return in the if block, both messages would print when the boss is alive:
def show_status(boss_health):
if boss_health > 0:
print("Ganondorf is alive!")
print("Ganondorf is unalive!")
That would output:
Ganondorf is alive!
Ganondorf is unalive!
Indentation is what tells Python whether code belongs to the if block or not. And don’t forget the colon (:) after your condition — it’s required.
How Does If/Else Work in Python?
An else block runs when the if condition is False. It gives you a clean two-way branch:
if score > high_score:
print("High score beat!")
else:
print("Better luck next time")
Some quick rules:
- You can’t have an
elsewithout anif - You can have an
ifwithout anelse
The else block is optional — but when you need exactly two branches, it’s cleaner than using a second if with the opposite condition. For simple two-way assignments, you can also use a ternary operator.
What Is Elif in Python?
elif stands for “else if”. It lets you chain multiple conditions together:
if score > high_score:
print("High score beat!")
elif score > second_highest_score:
print("You got second place!")
elif score > third_highest_score:
print("You got third place!")
else:
print("Better luck next time")
Python evaluates these top to bottom. The first condition that’s True has its body executed — and all the rest are skipped. If none of the if or elif conditions are True, the else block runs.
Here’s a practical example — a function that returns a player’s status:
def player_status(health):
if health <= 0:
return "dead"
elif health <= 5:
return "injured"
else:
return "healthy"
Order matters. If you checked health <= 5 first, dead players (with health 0 or below) would be called “injured” instead.
How Do You Combine Multiple Conditions With And and Or?
Sometimes one condition isn’t enough. Python’s and and or operators let you combine boolean expressions.
and returns True only if both sides are True:
def is_dog(num_legs, weight):
return num_legs == 4 and weight < 100
Let’s trace through is_dog(4, 99):
return 4 == 4 and 99 < 100
return True and True
return True
And is_dog(3, 98):
return 3 == 4 and 98 < 100
return False and True
return False
or returns True if at least one side is True:
def is_car_cool(speed, is_electric):
return speed > 200 or is_electric
You can use parentheses to control the order of operations, just like in math:
should_admit = (high_gpa and high_sat_score) or is_rich
What Are Guard Clauses in Python?
When you have multiple conditions that must all be true, it’s tempting to nest your if statements:
def check_conditions(condition_1, condition_2, condition_3):
if condition_1:
if not condition_2:
if condition_3 > 1:
return True
return False
That’s hard to read. A better approach is to invert each condition and return early — these are called guard clauses:
def should_serve_drinks(age, is_working, time):
if age < 21:
return False
if not is_working:
return False
if time < 5 or time > 10:
return False
return True
Each if block handles one reason to bail out. If you make it past all the guards, you know every condition was met. This pattern keeps your code flat and readable — no deeply nested indentation to parse.
One more thing: if statements don’t need an explicit comparison when checking booleans. These are identical:
if is_big:
# ...
if is_big == True:
# ...
Prefer the first — the == True is redundant.
What Should You Learn After Python Conditionals?
Now that you can make your programs branch and decide, the next big step is loops — running code repeatedly until a condition changes. Loops and conditionals together are the foundation of all control flow. You’ll also want to learn about lists soon, since loops and lists go hand in hand.
If you want to keep going through the full Python curriculum with hands-on exercises, check out the Learn to Code in Python course on Boot.dev.
Frequently Asked Questions
What is the difference between if, elif, and else in Python?
An if statement checks the first condition. An elif (short for else if) checks an additional condition only when the previous conditions were False. An else block runs only when none of the if or elif conditions were True.
Can you have multiple elif statements in Python?
Yes. You can chain as many elif statements as you need between the if and the optional else. Python evaluates them top to bottom and runs the first one that is True.
What happens if no condition is True in an if/elif chain?
If there is an else block, that code runs. If there is no else, nothing happens and execution continues after the entire if/elif block.
How do comparison operators differ from logical operators in Python?
Comparison operators like ==, !=, <, and > compare two values and return a single boolean. Logical operators like and and or combine multiple boolean expressions into one.
Related Articles
Python Math: Operators and Numbers Explained
Mar 02, 2026 by lane
Python has excellent built-in support for math operations — from basic arithmetic to exponents to bitwise logic. You don’t need to import anything to do most math in Python, which is one reason it’s so popular for everything from backend development to data science.
Python Functions: How to Define and Call Them
Mar 01, 2026 by lane
Functions allow you to reuse and organize code. Instead of copying and pasting the same logic everywhere, you define it once and call it whenever you need it. They’re the most important tool for writing DRY code.
Python Variables: A Complete Beginner Guide
Feb 28, 2026 by lane
Variables are how we store data as our program runs. You’re probably already familiar with printing data by passing it straight into print(), but variables let us save that data so we can reuse it and change it before printing it. This guide covers everything you need to know about Python variables: creating them, naming them, and understanding the basic data types they can hold. If you’re just getting started, you might also want to know why Python is worth learning in the first place.
Queue Data Structure in Python: Ordering at Its Best
Oct 22, 2023 by lane
A queue is an efficient collection of ordered items. New items can be added to one side, and removed from the other side.