If, Elif, Else in Python – Complete Beginner’s Guide with Examples
🔀 Python Conditional Statements (if, elif, else) Explained for Beginners
Introduction
In the previous lesson, you learned about Python operators. Now it’s time to use them in real decision-making.
Imagine you’re building:
-
A login system
-
A voting eligibility checker
-
A grading calculator
-
A shopping discount app
All these programs need to make decisions.
This is where conditional statements come in.
Conditional statements allow your program to:
-
Check a condition
-
Decide what to do
-
Execute different code based on the result
In Python, we use:
-
if -
elif -
else
Let’s understand them step by step. GeeksforGeeks
What is a Conditional Statement?
A conditional statement checks whether a condition is True or False.
If the condition is True → code runs
If False → code is skipped (or another block runs)
Basic structure:
if condition:
# code to execute
Notice the indentation (space before code). Python uses indentation to define blocks.
1️⃣ The if Statement
The if statement runs code only when a condition is True.
Example:
age = 20
if age >= 18:
print("You are eligible to vote.")
Output:
You are eligible to vote.
If age were 15, nothing would print.
Real-Life Example
Think of it like this:
"If it rains, take an umbrella."
rain = True
if rain:
print("Take an umbrella.")
Simple and powerful.
2️⃣ The else Statement
Sometimes you want your program to do something if the condition is False.
That’s where else comes in.
Example:
age = 16
if age >= 18:
print("You can vote.")
else:
print("You are not eligible to vote.")
Output:
You are not eligible to vote.
Now your program handles both situations.
3️⃣ The elif Statement (Else If)
What if you have multiple conditions?
Example:
-
Score above 90 → Grade A
-
Score above 75 → Grade B
-
Score above 50 → Grade C
-
Otherwise → Fail
You use elif.
Example:
score = 82
if score >= 90:
print("Grade A")
elif score >= 75:
print("Grade B")
elif score >= 50:
print("Grade C")
else:
print("Fail")
Output:
Grade B
Python checks conditions one by one from top to bottom.
Flow of Execution
-
Python checks the
ifcondition -
If True → runs that block and skips others
-
If False → checks
elif -
If none match → runs
else
Very logical and easy to understand.
Using Logical Operators in Conditions
You can combine conditions using:
-
and -
or -
not
Example:
age = 20
citizen = True
if age >= 18 and citizen:
print("Eligible to vote.")
else:
print("Not eligible.")
Here both conditions must be True.
Nested if Statements
You can place an if inside another if.
Example:
age = 22
has_license = True
if age >= 18:
if has_license:
print("You can drive.")
else:
print("You need a license.")
else:
print("You are too young to drive.")
This is called nested if.
Mini Project 1: Even or Odd Checker
number = int(input("Enter a number: "))
if number % 2 == 0:
print("Even number")
else:
print("Odd number")
This program uses:
-
Modulus operator
% -
if-else condition
Mini Project 2: Simple ATM Withdrawal
balance = 5000
withdraw = int(input("Enter amount to withdraw: "))
if withdraw <= balance:
balance -= withdraw
print("Transaction successful.")
print("Remaining balance:", balance)
else:
print("Insufficient balance.")
This is how real banking systems use conditions.
Important Rules of Conditional Statements
✅ Indentation is mandatory
✅ Use colon : after condition
✅ Only one else per if block
✅ You can have multiple elif
Incorrect:
if age > 18
print("Adult")
Correct:
if age > 18:
print("Adult")
Common Beginner Mistakes
❌ Forgetting colon :
❌ Incorrect indentation
❌ Using = instead of ==
❌ Writing conditions in wrong order
Example mistake:
score = 95
if score >= 50:
print("Pass")
elif score >= 90:
print("Grade A")
This will never print Grade A because first condition already matches.
Correct order:
if score >= 90:
print("Grade A")
elif score >= 50:
print("Pass")
Ternary (Short If-Else)
Python allows short version:
age = 20
print("Adult") if age >= 18 else print("Minor")
Useful for small conditions.
Real-World Applications of Conditional Statements
Conditional statements are used in:
-
Login systems
-
Payment gateways
-
E-commerce discounts
-
AI decision models
-
Game development
-
Security systems
Without conditions, programs cannot think or decide.
Practice Exercises for Readers
Encourage your readers to try:
-
Write a program to check whether a number is positive, negative, or zero.
-
Create a password checker (minimum 8 characters).
-
Build a temperature checker:
-
Above 35 → “Hot”
-
Between 20–35 → “Normal”
-
Below 20 → “Cold”
-
Adding exercises increases engagement and SEO time-on-page.
Conclusion
Conditional statements (if, elif, else) allow your Python programs to make decisions.
Today you learned:
-
Basic if statement
-
if-else
-
elif for multiple conditions
-
Nested if
-
Logical operators with conditions
-
Real-world mini projects
Now your programs can think and respond intelligently.
👉 In the next lesson, we’ll explore Loops in Python (for and while loops) — where your program can repeat tasks automatically.
Keep practicing daily — consistency builds coding confidence. 🚀
Python Variables and Data Types
🧮 Python Operators Explained with Examples (Beginner Friendly Guide)
Comments