Control flow is essential in any programming language. Python uses if
, elif
, and else
statements to make decisions in your programs based on conditions.
In this tutorial, you’ll learn:
-
The basic structure of
if
,elif
, andelse
-
How conditions work
-
Nested and chained conditions
-
Common mistakes
-
Tips and tricks
-
A complete example at the end
What Is if...else
?
Python's if...else
statements let you run code based on whether a condition is true or false.
✅ Basic Structure
if condition:
# code to run if condition is true
elif another_condition:
# code to run if another_condition is true
else:
# code to run if no conditions are true
Example 1: Simple if
Statement
x = 10
if x > 5:
print("x is greater than 5")
Output:
x is greater than 5
Example 2: if...else
Statement
x = 3
if x > 5:
print("x is greater than 5")
else:
print("x is 5 or less")
Example 3: if...elif...else
score = 85
if score >= 90:
print("Grade: A")
elif score >= 80:
print("Grade: B")
elif score >= 70:
print("Grade: C")
else:
print("Grade: F")
Understanding Boolean Conditions
Python evaluates conditions as either True or False.
# Comparisons
x == 5 # True if x equals 5
x != 10 # True if x does not equal 10
x > 3 # Greater than
x < 7 # Less than
x >= 3 # Greater than or equal
x <= 9 # Less than or equal
# Logical operators
x > 3 and x < 7 # True if both are True
x == 5 or x == 10 # True if either is True
not x == 5 # True if x is NOT 5
Nested if
Statements
You can place an if
inside another if
:
age = 20
has_id = True
if age >= 18:
if has_id:
print("Access granted.")
else:
print("ID required.")
else:
print("Access denied.")
Chained Conditions
You can use logical operators (and
, or
, not
) to write complex conditions in a single line.
x = 25
if x > 10 and x < 50:
print("x is between 10 and 50")
One-Liner if...else
(Ternary Operator)
You can write simple if...else
expressions in one line:
age = 18
status = "Adult" if age >= 18 else "Minor"
print(status)
Common Pitfalls
Pitfall | Why It Happens | Solution |
---|---|---|
Using = instead of == |
= is assignment, not comparison |
Use == in conditions |
Missing colons (: ) |
SyntaxError | Always add : after condition |
Indentation Errors | Python uses indentation for blocks | Use consistent 4-space indent |
Overlapping Conditions | Conditions may overlap | Order matters; use elif wisely |
Overusing Nested if s |
Reduces readability | Use logical operators instead |
Tips
-
Use
elif
to avoid unnecessary nesting. -
Always cover all logical cases with
else
as a fallback. -
Use parentheses
()
for clarity in complex conditions. -
Use comments to explain condition logic for beginners.
-
Test edge cases (e.g.,
==
,>=
,<=
) to avoid logic bugs.
Complete Example: Voting Eligibility
age = int(input("Enter your age: "))
citizen = input("Are you a citizen? (yes/no): ").lower()
if age >= 18:
if citizen == "yes":
print("You are eligible to vote.")
else:
print("You must be a citizen to vote.")
else:
print("You must be at least 18 years old to vote.")
What’s Next?
After mastering if...else
, explore:
-
While and for loops
-
Logical operators in depth
-
Exception handling with
try...except
-
Functions to modularize your logic
Practice Challenge
Task: Write a program that checks if a number is positive, negative, or zero.
number = float(input("Enter a number: "))
if number > 0:
print("Positive")
elif number < 0:
print("Negative")
else:
print("Zero")