-
Hajipur, Bihar, 844101
Every program needs a way to make decisions. An If…Else statement is the simplest and most common way to do that in Python. It checks whether a condition is true. If it is, the program runs a certain block of code. If it isn’t, the program can choose another path. This idea appears everywhere: checking login details, verifying age limits, applying discounts, or responding to user choices.
In daily Python work, If…Else becomes second nature. You will write it so often that you’ll start thinking in conditions automatically. But the foundation remains simple: "If this happens, then do this. Otherwise, do something else."
A basic If statement has two important parts:
A condition
An indented block of code
Example:
age = 20
if age >= 18:
print("You can vote.")
Here, the program checks whether age >= 18. If the condition is true, it prints the message. If not, it silently skips the block.
Indentation is not optional. Python reads indentation as structure. This is one of the most common mistakes for beginners, so get into the habit of using four spaces.
When you need more than one possible outcome, you add elif and else.
Use elif when you want to test another condition after the first one fails.
Use else when no previous condition matched and you want a final fallback.
Example:
marks = 72
if marks >= 90:
print("Grade A")
elif marks >= 75:
print("Grade B")
elif marks >= 60:
print("Grade C")
else:
print("Grade D")
This structure allows the program to pick exactly one path. Only the first true condition runs its block. The rest are ignored.
To avoid common mistakes, always keep these points in mind:
Add a colon after if, elif and else.
Use consistent indentation. Four spaces is standard.
Don’t use = when you mean ==.
Keep conditions readable and simple.
Write logical checks using and, or and not when needed.
Python is sensitive to spacing. Even one wrong space can break your program. Always format conditions clearly so it’s easy to see what you are checking.
Conditions often involve checks like equality, comparing values, or combining multiple conditions.
| Type | Operators | Example |
|---|---|---|
| Comparison | ==, !=, >, <, >=, <= |
age >= 18 |
| Logical | and, or, not |
age > 18 and citizen == True |
| Membership | in, not in |
"red" in colors |
You’ll use these operators in almost every decision you write.
Python lets you combine conditions in a single line. This makes your code shorter and easier to follow.
Example:
age = 20
country = "India"
if age >= 18 and country == "India":
print("Eligible for voter ID.")
This condition checks two things at once. If both are true, the message prints.
If you find yourself writing too many and and or combinations, consider breaking logic into smaller chunks or using a function.
Beginners often run into the same problems:
Using = inside conditions instead of ==.
Forgetting a colon after the condition line.
Mixing tabs and spaces.
Placing conditions in the wrong order.
Comparing input without converting it to a number.
Most errors come from indentation or comparison mistakes. When you see an error message pointing to an if line, the issue is usually above it.
User input needs casting because input() always returns a string. If you want numeric comparison, you must convert it.
Steps:
Ask for input
Cast to a number
Apply the condition
Handle invalid input
Example:
try:
number = int(input("Enter a number: "))
if number % 2 == 0:
print("Even number.")
else:
print("Odd number.")
except ValueError:
print("Please enter a valid integer.")
This simple pattern appears in many beginner projects.
You nest conditions when one check depends on another. Keep nesting light. Too many levels make the code messy.
Example:
age = 25
citizen = True
if age >= 18:
if citizen:
print("You can apply.")
else:
print("Only citizens can apply.")
else:
print("You must be at least 18.")
Use nesting only when it logically makes sense.
Here’s a small example showing multiple checks and different outcomes:
username = "sara"
password = "1234"
user = input("Enter username: ")
pwd = input("Enter password: ")
if user == username and pwd == password:
print("Login successful.")
elif user == username:
print("Incorrect password.")
else:
print("User not found.")
This approach is common in authentication, forms, dashboards and many basic projects.
Write clear conditions — avoid cramming too much into one line.
Use readable variable names.
Place simple checks first and complex checks later.
Test boundary values, like equal limits (18, 100, 0).
Keep each block short; long logic belongs in functions.
If…Else is one of the most important tools in Python. It lets your program pick between multiple paths based on conditions. Use if for the first check, elif for extra checks and else for the fallback. Focus on indentation, readable conditions and correct operators. Whether you’re validating user input, checking permissions or creating a small feature, If…Else makes your code flexible and interactive.
Write a program that asks the user for their age and prints whether they are a minor, an adult, or a senior citizen.
Take a number from the user and print whether it is positive, negative, or zero.
Ask the user to enter a password. If it matches "admin123", print “Access granted.” Otherwise show “Access denied.”
Create a script that takes two numbers from the user and prints the larger one.
Write a program that checks if a user’s entered username matches "sara". If it matches, check the password; if not, print “Unknown user.”
Ask the user for a mark between 0 and 100. Print the grade based on ranges using if, elif, and else.
Take an integer input and print whether it is even or odd.
Ask for a user’s country and age. If the country is “India” and age is 18 or above, print “Eligible for voter ID.” Otherwise print the reason.
Write a program that checks if a number is divisible by both 3 and 5, only 3, only 5, or none.
Create a small menu system: ask the user to enter 1, 2, or 3. Print a different message for each number using If…Elif…Else.