PYTHON IF ELSE & ELIF

Control program execution flow dynamically using conditional statements, comparisons, and real-world business logic rules.

🔀 What are If, Elif, and Else Statements in Python?

Python if, elif (else if), and else statements enable programs to make decisions. Python evaluates conditional expressions and executes the appropriate indented code block. They are foundational for login validation, shopping discounts, banking systems, payroll calculation, and user access control.

1. Basic If-Elif-Else Syntax & Age Classification
Classify user age into distinct categories using sequential condition checks.
Python
age = 25

if age >= 18:
    print("Adult")
elif age >= 13:
    print("Teenager")
else:
    print("Child")
Output: Adult
Real-Time Example 1: Login Authentication System
Validate username, password, and account activation status before granting system access.
Python
username = "admin"
password = "python123"
account_active = True

if username == "admin" and password == "python123":
    if account_active:
        print("Login successful! Welcome administrator.")
    else:
        print("Account is inactive.")
else:
    print("Invalid username or password.")
Output: Login successful! Welcome administrator.
Real-Time Example 2: E-Commerce Order Discount Calculation
Calculate discount percentages tiered according to the customer's shopping order total.
Python
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(f"Discount Applied: {discount}%")
print(f"Final Amount Payable: ₹{final_amount}")
Output: Discount Applied: 10% Final Amount Payable: ₹6750.0
Real-Time Example 3: ATM Withdrawal Validation
Validate withdrawal amounts against account balances and ATM note multiples before dispensing cash.
Python
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 -= withdraw_amount
    print("Withdrawal successful!")
    print(f"Remaining Balance: ₹{balance}")
Output: Withdrawal successful! Remaining Balance: ₹17000
Real-Time Example 4: Employee Performance Bonus Calculation
Determine annual salary increment percentages based on employee performance ratings.
Python
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_amount = salary * bonus_percent / 100
print(f"Bonus Percentage: {bonus_percent}%")
print(f"Bonus Payout: ₹{bonus_amount}")
Output: Bonus Percentage: 20% Bonus Payout: ₹12000.0
Real-Time Example 5: Student Exam Grade Evaluation
Convert student percentage marks into letter grades for report card generation.
Python
percentage = 82

if percentage >= 90:
    grade = "A+"
elif percentage >= 80:
    grade = "A"
elif percentage >= 70:
    grade = "B"
elif percentage >= 60:
    grade = "C"
else:
    grade = "Fail"

print(f"Percentage: {percentage}% | Grade: {grade}")
Output: Percentage: 82% | Grade: A
Real-Time Example 6: Delivery Charge Calculator
Calculate shipping fees based on order cart totals for online retail delivery.
Python
order_total = 850

if order_total >= 1000:
    delivery_charge = 0
elif order_total >= 500:
    delivery_charge = 40
else:
    delivery_charge = 70

print(f"Order Total: ₹{order_total} | Delivery Charge: ₹{delivery_charge}")
Output: Order Total: ₹850 | Delivery Charge: ₹40
Real-Time Example 7: Weather Advisory Recommendation
Display advice and clothing recommendations according to current weather temperatures.
Python
temperature = 34

if temperature >= 40:
    message = "Extreme heat warning - stay indoors."
elif temperature >= 30:
    message = "Hot weather - stay hydrated."
elif temperature >= 20:
    message = "Pleasant weather conditions."
else:
    message = "Cool weather - carry a jacket."

print("Weather Advisory:", message)
Output: Weather Advisory: Hot weather - stay hydrated.
Real-Time Example 8: Nested If – Driving License Eligibility
Use nested if statements when a validation check depends on multiple hierarchical criteria.
Python
age = 25
has_license = True

if age >= 18:
    if has_license:
        print("You are eligible to drive.")
    else:
        print("You must acquire a driving license first.")
else:
    print("You are underage to drive.")
Output: You are eligible to drive.
Real-Time Example 9: Loan Qualification Check (Logical AND/OR)
Combine multiple conditions using and and or operators for financial loan eligibility.
Python
age = 30
monthly_salary = 45000
credit_score = 720

if age >= 21 and monthly_salary >= 30000 and credit_score >= 700:
    print("Loan Application Approved!")
else:
    print("Loan Application Rejected due to criteria mismatch.")
Output: Loan Application Approved!