Python Conditional Statements Tutorial | if, elif, else with Examples
One of the most powerful features of programming is the ability to make decisions based on different situations. In Python, conditional statements allow programs to execute different blocks of code depending on whether a condition is true or false.
From login authentication and ATM systems to AI-powered applications and e-commerce platforms, decision-making is an essential part of modern software development. Python provides simple yet powerful conditional statements such as if, elif, and else to control program flow.
In this lesson, you’ll learn how to create decision-making logic using conditional statements, combine multiple conditions with logical operators, write nested conditions, and solve real-world programming problems through practical examples.
By the end of this lesson, you’ll be able to build intelligent programs that respond dynamically to user input and different scenarios.
Learning Objectives
After completing this lesson, you will be able to:
- Understand conditional statements.
- Use the
ifstatement. - Use
if...else. - Work with
if...elif...else. - Create nested conditions.
- Combine conditions using logical operators.
- Write decision-making programs.
- Avoid common mistakes in conditional programming.
Topics Covered
- Introduction to Conditional Statements
- Boolean Expressions
- The
ifStatement - The
if...elseStatement - The
if...elif...elseStatement - Nested
ifStatements - Logical Operators with Conditions
- Real-World Examples
- Best Practices
Detailed Lesson Content
Introduction to Conditional Statements
Imagine you are using an ATM. The machine first checks:
- Is the PIN correct?
- Does the account have enough balance?
- Is the withdrawal amount within the daily limit?
Depending on these conditions, the ATM either allows the transaction or displays an error message.
Similarly, online shopping websites check whether:
- A product is in stock.
- A coupon is valid.
- The customer qualifies for free shipping.
- Payment has been completed successfully.
These decisions are made using conditional statements.
A conditional statement evaluates a condition and executes different blocks of code based on whether the condition is True or False.
What is a Condition?
A condition is an expression that returns either:
- True
- False
Example:
age = 20
print(age >= 18)
Output
True
Python uses these Boolean values to decide which code should execute.
The if Statement
The simplest conditional statement is the if statement.
Syntax
if condition:
# Code to execute
Example:
age = 20
if age >= 18:
print("You are eligible to vote.")
Output
You are eligible to vote.
If the condition is False, nothing happens.
Understanding Indentation
Python uses indentation (spaces) instead of braces {}.
Correct:
marks = 80
if marks >= 50:
print("Pass")
Incorrect:
if marks >= 50:
print("Pass")
Python will generate an IndentationError.
The if…else Statement
Sometimes we need two possible outcomes.
Example:
age = 16
if age >= 18:
print("Eligible")
else:
print("Not Eligible")
Output
Not Eligible
Flow:
- If condition is True → execute
if - Otherwise → execute
else
The if…elif…else Statement
When multiple conditions need to be checked, use elif.
Example:
marks = 82
if marks >= 90:
print("Grade A+")
elif marks >= 75:
print("Grade A")
elif marks >= 60:
print("Grade B")
else:
print("Grade C")
Output
Grade A
Python checks conditions from top to bottom and stops when the first True condition is found.
Nested if Statements
An if statement can exist inside another if statement.
Example:
age = 22
citizen = True
if age >= 18:
if citizen:
print("Eligible to Vote")
else:
print("Citizen Verification Required")
else:
print("Not Eligible")
Output
Eligible to Vote
Nested conditions are useful when multiple checks depend on one another.
Using Logical Operators
Logical operators allow us to combine multiple conditions.
AND Operator
Both conditions must be True.
age = 25
experience = 3
if age >= 21 and experience >= 2:
print("Eligible")
OR Operator
Only one condition needs to be True.
marks = 92
if marks >= 90 or marks >= 85:
print("Scholarship Eligible")
NOT Operator
Reverses the result.
is_blocked = False
if not is_blocked:
print("Login Successful")
Real-World Example 1: Login System
username = input("Username: ")
password = input("Password: ")
if username == "admin" and password == "12345":
print("Login Successful")
else:
print("Invalid Username or Password")
Real-World Example 2: ATM Withdrawal
balance = 25000
withdraw = int(input("Enter Amount: "))
if withdraw <= balance:
balance = balance - withdraw
print("Transaction Successful")
print("Remaining Balance:", balance)
else:
print("Insufficient Balance")
Real-World Example 3: Student Grading System
marks = int(input("Enter Marks: "))
if marks >= 90:
print("Grade A+")
elif marks >= 80:
print("Grade A")
elif marks >= 70:
print("Grade B")
elif marks >= 60:
print("Grade C")
else:
print("Fail")
Real-World Example 4: E-commerce Discount
bill = float(input("Enter Bill Amount: "))
if bill >= 5000:
discount = bill * 0.20
elif bill >= 3000:
discount = bill * 0.10
else:
discount = 0
final_bill = bill - discount
print("Discount:", discount)
print("Final Bill:", final_bill)
Common Mistakes
Forgetting the Colon
Incorrect
if age >= 18
Correct
if age >= 18:
Incorrect Indentation
if age >= 18:
print("Eligible")
Correct
if age >= 18:
print("Eligible")
Using = Instead of ==
Incorrect
if age = 18:
Correct
if age == 18:
Best Practices
- Write simple and readable conditions.
- Avoid deeply nested conditions whenever possible.
- Use meaningful variable names.
- Use logical operators efficiently.
- Test all possible outcomes.
- Handle invalid user input gracefully.
Key Takeaways
- Conditional statements control program flow.
ifexecutes code when a condition is True.elsehandles alternative scenarios.elifallows multiple conditions.- Nested
ifstatements support complex decision-making. - Logical operators combine multiple conditions.
- Proper indentation is mandatory in Python.



