🔀 What are If, Else and Elif in Python?
Python if, elif, and else statements allow a program to make decisions. Python evaluates a condition and executes the appropriate block of code. This is commonly used in login systems, shopping applications, banking systems, payroll software, education platforms, and many other real-world applications.
if
Checks the first condition. If it is True, the associated code is executed.
if age >= 18:
print("Adult")
elif
Means "else if". It allows the program to test another condition.
elif age >= 13:
print("Teenager")
else
Executes when none of the previous conditions are True.
else:
print("Child")
if condition1:
# code for condition1
elif condition2:
# code for condition2
else:
# default code
age = 25
if age >= 18:
print("Adult")
elif age >= 13:
print("Teenager")
else:
print("Child")
username = "admin"
password = "python123"
account_active = True
if username == "admin" and password == "python123":
if account_active:
print("Login successful")
else:
print("Account is inactive")
else:
print("Invalid username or password")
order_amount = 7500
if order_amount >= 10000:
discount = 20
elif order_amount >= 5000:
discount = 10
elif order_amount >= 2000:
discount = 5
else:
discount = 0
discount_amount = order_amount * discount / 100
final_amount = order_amount - discount_amount
print("Discount:", discount, "%")
print("Final Amount:", final_amount)
balance = 25000
withdraw_amount = 8000
if withdraw_amount <= 0:
print("Invalid withdrawal amount")
elif withdraw_amount > balance:
print("Insufficient balance")
elif withdraw_amount % 100 != 0:
print("Amount must be a multiple of 100")
else:
balance = balance - withdraw_amount
print("Withdrawal successful")
print("Remaining balance:", balance)
rating = 4.5
salary = 60000
if rating >= 4.5:
bonus_percent = 20
elif rating >= 4.0:
bonus_percent = 15
elif rating >= 3.0:
bonus_percent = 10
else:
bonus_percent = 5
bonus = salary * bonus_percent / 100
print("Bonus Percentage:", bonus_percent)
print("Bonus Amount:", bonus)
percentage = 82
if percentage >= 90:
grade = "A+"
elif percentage >= 80:
grade = "A"
elif percentage >= 70:
grade = "B"
elif percentage >= 60:
grade = "C"
elif percentage >= 50:
grade = "D"
else:
grade = "Fail"
print("Percentage:", percentage)
print("Grade:", grade)
order_total = 850
if order_total >= 1000:
delivery_charge = 0
elif order_total >= 500:
delivery_charge = 40
else:
delivery_charge = 70
print("Delivery Charge:", delivery_charge)
temperature = 34
if temperature >= 40:
message = "Extreme heat - stay indoors"
elif temperature >= 30:
message = "Hot weather - stay hydrated"
elif temperature >= 20:
message = "Pleasant weather"
else:
message = "Cool weather - carry a jacket"
print(message)
age = 25
has_license = True
if age >= 18:
if has_license:
print("You can drive")
else:
print("You need a driving license")
else:
print("You are not eligible to drive")
Using AND
All conditions connected with and must be True.
age = 25
salary = 50000
if age >= 21 and salary >= 30000:
print("Eligible for the offer")
else:
print("Not eligible")
Using OR
At least one condition connected with or must be True.
is_admin = False
is_manager = True
if is_admin or is_manager:
print("Access granted")
else:
print("Access denied")
if
Used to test the first condition. It starts the decision-making block.
if score >= 90:
print("Excellent")
elif
Checks another condition when the previous condition was False.
elif score >= 60:
print("Passed")
else
Handles the default case when all previous conditions are False.
else:
print("Failed")
Missing Colon
# Incorrect
if age >= 18
print("Adult")
# Correct
if age >= 18:
print("Adult")
Incorrect Indentation
# Incorrect
if age >= 18:
print("Adult")
# Correct
if age >= 18:
print("Adult")
Keep Conditions Readable
if age >= 18 and has_license:
print("Eligible")
Order Conditions Carefully
if score >= 90:
grade = "A"
elif score >= 75:
grade = "B"
elif score >= 60:
grade = "C"
else:
grade = "D"
Use Else as the Default Case
if status == "active":
print("User active")
else:
print("User unavailable")
Avoid Unnecessary Nesting
if age >= 18 and has_license:
print("Can drive")