- Courses
- 💻Programming Languages
- 🐍Python
- Control Flow
- If/Else Statements
On this page
If/Else Statements
By Enrico Hochkoeppler
Web Developer & TutoriaLibre Founder
What Conditional Statements Are
Conditional statements allow a program to make decisions. Instead of executing every instruction in order, the program evaluates a condition and chooses which block of code to run. This is the foundation of all control flow in Python.
A condition is any expression that evaluates to a Boolean value (True or False). If the condition is True, Python executes the associated block. If it is False, Python skips it.
The if Statement
An if statement executes a block of code only when its condition is True.
age = 20
if age >= 18:
print("You are an adult.")
If the condition is false, the block is ignored.
Indentation is mandatory. Python uses indentation to define code blocks.
The else Clause
An else clause provides an alternative block that runs when the if condition is False.
age = 15
if age >= 18:
print("You are an adult.")
else:
print("You are a minor.")
Only one of the two blocks will run.
The elif Clause
elif (short for “else if”) allows you to check multiple conditions in sequence. Python evaluates them from top to bottom and executes the first one that is True.
score = 72
if score >= 90:
print("Grade: A")
elif score >= 80:
print("Grade: B")
elif score >= 70:
print("Grade: C")
else:
print("Grade: D")
Only one branch runs, even if multiple conditions could be true.
How Python Evaluates Conditions
Python evaluates each condition in order:
- Evaluate the
ifcondition - If it is
True, run its block and skip the rest - If it is
False, evaluate the nextelif - If no condition is
True, run theelseblock (if present)
This top‑down evaluation ensures predictable behavior.
Using Expressions Inside Conditions
Conditions can use any expression that produces a Boolean value. This includes comparisons, logical operators, and truthiness.
name = input("Enter your name: ")
if name:
print("Hello,", name)
else:
print("You did not enter a name.")
An empty string is considered False, so the else block runs when the user presses Enter without typing anything.
Nested Conditions
You can place one conditional inside another. This is called nesting.
age = 25
has_id = True
if age >= 18:
if has_id:
print("Entry allowed.")
else:
print("ID required.")
else:
print("Entry denied.")
Nesting is useful but should be used carefully to maintain readability.
Why If/Else Statements Matter
If/else statements allow programs to react to different situations, validate input, enforce rules, and choose between alternative actions. They are essential for building interactive programs, implementing algorithms, and controlling the flow of execution throughout your Python applications.
Found this helpful? Share the knowledge...
... or support this project to keep it alive!
Previous
Boolean Logic
Next
Match/Case Statements
- What Conditional Statements Are
- The
ifStatement - The
elseClause - The
elifClause - How Python Evaluates Conditions
- Using Expressions Inside Conditions
- Nested Conditions
- Why If/Else Statements Matter
