if, elif, and else statements
These statements let your program make decisions based on conditions — they’re the core of control flow in Python. For beginners, see our Introduction to Python lesson to get started, and our Loops and Iteration module for how these conditions interact with repeatable tasks.
Basic Syntax
if condition:
# code block if condition is True
elif another_condition:
# code block if elif is True
else:
# code block if none are True
- Each condition is checked in order
- Indentation (typically 4 spaces) defines code blocks
- You can have one if, zero or more elif, and zero or one else
Simple Example:
temperature = 25
if temperature > 30:
print(“It’s hot outside.”)
elif temperature >= 20:
print(“It’s a nice day.”)
else:
print(“It’s cold outside.”)
Output: “It’s a nice day.”
Flow Explanation:
- If the if condition is True, it runs that block and skips the rest.
- If the if is False, Python checks the elif (if any).
- If none of the conditions are True, the else block runs.
Real-Life Example: Age Check
age = int(input(“Enter your age: “))
if age < 13:
print(“You are a child.”)
elif age < 18:
print(“You are a teenager.”)
else:
print(“You are an adult.”)
Nestings and Practical Use
You can nest conditions inside others:
score = 85
if score >= 50:
print(“You passed!”)
if score >= 80:
print(“Great job!”)
else:
print(“You failed.”)
Common Mistakes to Avoid
| Mistake | Fix |
|---|---|
Missing colon : |
Add a colon at end of conditionif x == 5 → if x == 5: |
| Improper indentation | Use consistent 4-space indentationprint("Hello") → print("Hello") |
Using = instead of == |
= is assignment, == is comparisonif x = 5: → if x == 5: |
Forgetting to cast input() to int |
Use int(input()) if comparing numbersage = input() → age = int(input()) |
Quick Quiz
What will this print?
x = 10
if x > 20:
print(“A”)
elif x > 5:
print(“B”)
else:
print(“C”)
Answer: “B”
Summary Table:
| Statement | Purpose |
|---|---|
if |
Starts a conditional check |
elif |
Checks another condition if the previous if or elif fails |
else |
Runs if no if or elif conditions are met |
Truthy and falsy values
In Python, every value has an inherent Boolean value — either True or False — even if it’s not literally the TrueorFalse` Boolean type. For a deeper understanding of how Python determines truthiness, check our Variables, Data Types and Operators lesson.
These values are evaluated when used in conditions, such as in if statements. For more on how different types behave in Boolean contexts, our Introduction to Python overview is a great starting point.
What are “Truthy” and “Falsy”?
- Truthy: A value that evaluates to True when used in a Boolean context.
- Falsy: A value that evaluates to False.
Example:
if “hello”:
print(“This is truthy.”)
Output: This is truthy.
- Because a non-empty string (“hello”) is considered truthy.
Falsy Values in Python
These values are considered False in Boolean contexts:
| Type | Falsy Value |
|---|---|
| Boolean | False |
| NoneType | None |
| Numeric | 0, 0.0, 0j |
| Sequence/Collection | '' (empty string),[] (empty list),() (empty tuple),{} (empty dict),set() (empty set) |
| Custom object | Any object with __bool__() or __len__() returning False |
# All of these evaluate as False
if not “”:
print(“Empty string is falsy”)
if not 0:
print(“Zero is falsy”)
if not None:
print(“None is falsy”)
if not []:
print(“Empty list is falsy”)
Truthy Values in Python
Everything else not listed as falsy is truthy.
Examples:
“hello” # non-empty string
42 # any non-zero number
[1, 2, 3] # non-empty list
{“a”: 1} # non-empty dictionary
True # obviously!
These will all make if statements run:
if [1, 2, 3]:
print(“Truthy list!”)
if “Python”:
print(“Truthy string!”)
Boolean Context Examples:
x = []
if x:
print(“List has items.”)
else:
print(“List is empty.”)
Output: List is empty.
Using bool() to Check Value Truthiness
You can test how Python evaluates any value using bool():
print(bool(0)) # False
print(bool(“Hi”)) # True
print(bool([])) # False
print(bool([0])) # True (non-empty list!)
Summary:
| Value Type | Truthy Example | Falsy Example |
|---|---|---|
| String | "Python" |
"" |
| Number | 42 |
0 |
| List | [1, 2] |
[] |
| Dictionary | {"a": 1} |
{} |
| Boolean | True |
False |
| NoneType | — | None |
Nested conditionals
What Are Nested Conditionals?
- A nested conditional is when you place an if, elif, or else inside another if block.
- This lets you check multiple layers of logic, like decision trees.
Basic Syntax:
if condition1:
if condition2:
# Runs if both condition1 AND condition2 are True
else:
# Runs if condition1 is True but condition2 is False
else:
# Runs if condition1 is False
Simple Example:
age = 20
has_ticket = True
if age >= 18:
if has_ticket:
print(“You can enter the event.”)
else:
print(“You need a ticket to enter.”)
else:
print(“You must be 18 or older to enter.”)
Output:
You can enter the event.
Why Use Nested Conditionals?
- When one condition depends on another
- To create multi-level decision structures
- When you want to check a second condition only if the first is True
Example: Student Grading System
grade = 85
if grade >= 50:
if grade >= 90:
print(“A+”)
elif grade >= 80:
print(“A”)
else:
print(“Pass”)
else:
print(“Fail”)
Output:
A
Alternative: Combine Conditions with and
You can sometimes flatten nested conditionals by using and:
if age >= 18 and has_ticket:
print(“You can enter.”)
elif age >= 18:
print(“Get a ticket.”)
else:
print(“You’re too young.”)
Both versions are valid — use nesting for clarity when conditions are hierarchical.
Common Mistakes to Avoid
| Mistake | Fix |
|---|




