Ever wondered how software knows what to do based on your input? For example:
- An ATM shows a message if you enter the wrong PIN.
- A grading app gives you a grade based on your score.
This is all possible through Conditional Statements — and Python makes it super simple!

What are Conditional Statements?
In Python, conditional statements allow your program to make decisions — run certain blocks of code only if a condition is met.
It’s like giving your code a brain — “If this is true, do this. Otherwise, do something else.”
The if
Statement
The simplest form of condition.
age = 20
if age >= 18:
print("You are eligible to vote.")
How it works:
- Python checks if
age >= 18
. - If True, it prints the message.
- If False, it does nothing.
👉 Note: Indentation (4 spaces or a tab) is required after if
to tell Python which lines belong inside the condition.
The if-else
Statement
Use this when you want to take one action if true, and another if false.
marks = 35
if marks >= 40:
print("You passed the exam!")
else:
print("You failed. Try again.")
This gives the program two paths: one for pass, one for fail.
The if-elif-else
Ladder
What if there are multiple options? Use elif
(short for else if).
score = 85
if score >= 90:
print("Grade: A")
elif score >= 75:
print("Grade: B")
elif score >= 60:
print("Grade: C")
else:
print("Grade: D")
Python checks conditions top to bottom, and runs the first one that is True
.
Also Read: Operators in Python
Operators Used in Conditions
Here are some important operators:
Operator | Meaning | Example (a = 10 , b = 5 ) |
---|---|---|
== | Equal to | a == b → False |
!= | Not equal to | a != b → True |
> | Greater than | a > b → True |
< | Less than | a < b → False |
>= | Greater or equal | a >= 10 → True |
<= | Less or equal | b <= 5 → True |
Nested if
Statements
You can put one if
inside another to check more complex logic.
username = input("Enter your username: ")
password = input("Enter your password: ")
if username == "admin":
if password == "1234":
print("Login successful!")
else:
print("Incorrect password!")
else:
print("Username not found!")
Common Mistakes to Avoid
Forgetting Indentation:
if age >= 18:
print("Adult") # ❌ Error!
✔️ Correct:
if age >= 18:
print("Adult")
Using =
Instead of ==
if x = 5: # ❌ Wrong (assignment, not comparison)
✔️ Correct:
if x == 5: # ✅
Mini Project
Project: Even or Odd Checker
Let’s apply what you’ve learned.
Task:
- Ask the user for a number
- Check if it’s even or odd
- Print the result
num = int(input("Enter a number: "))
if num % 2 == 0:
print("It is an Even number.")
else:
print("It is an Odd number.")
💡 %
is the modulus operator. It gives the remainder. If the remainder is 0
, the number is even.
Summary
Statement | Purpose |
---|---|
if | Run code only when condition is true |
if-else | Do one thing if true, another if false |
if-elif-else | Choose between multiple conditions |
nested if | Run conditions inside other conditions |
Project Challenge 1: Simple Grading System
Objective:
Create a Python program that takes a student’s marks (0–100) as input and displays the corresponding grade based on this scale:
Marks | Grade |
---|---|
90 – 100 | A+ |
75 – 89 | A |
60 – 74 | B |
40 – 59 | C |
0 – 39 | F |
Hints:
- Use
input()
andint()
to take marks - Use
if-elif-else
to determine the grade - Make sure to handle invalid inputs like marks above 100 or below 0 (optional challenge)
Sample Output:
Enter your marks: 82
You got Grade A
Project Challenge 2: Basic Salary Tax Calculator
Objective:
Create a program that calculates the income tax based on salary input using these slabs:
Salary Range (₹) | Tax Rate |
---|---|
0 – 2,50,000 | No Tax |
2,50,001 – 5,00,000 | 5% |
5,00,001 – 10,00,000 | 10% |
Above 10,00,000 | 20% |
Hints:
- Ask user to input their annual salary
- Use
if-elif-else
to check which range it falls into - Use multiplication (
*
) to calculate tax - Print the tax amount
Sample Output:
Enter your annual salary: 750000
You need to pay ₹75000.0 as income tax.
What’s Next?
In the next post, we’ll learn about the Loops in Python