Learn with Yasir

Share Your Feedback

Python if‑elif‑else Exercises – Practice Conditional Logic

Practice Python if‑elif‑else statements with beginner-friendly exercises. Strengthen your understanding of conditional logic, syntax, and control flow with hands-on coding problems.

Topic: if-elif-else


🧪 Coding Exercises

🟢 Beginner Exercises

  1. FizzBuzz
  2. Write a program that prints the numbers from 1 to n. 
    - But for multiples of 3, print "Fizz" instead of the number
    - and for the multiples of 5, print "Buzz". 
    - For numbers which are multiples of both 3 and 5, print "FizzBuzz".
    

    Requirements

    • Use if-elif-else structure
    • Handle numbers from 1 to n inclusive

    Input

    15

    Expected Output

    1
    2
    Fizz
    4
    Buzz
    Fizz
    7
    8
    Fizz
    Buzz
    11
    Fizz
    13
    14
    FizzBuzz
    

  3. Check Positive, Negative, or Zero
  4. Write a program to check if a number is positive, negative, or zero.
    

    Requirements

    • Use if-elif-else structure
    • Handle all three cases

    Input

    -5

    Expected Output

    Negative

  5. Even or Odd
  6. Write a program to check if a number is even or odd.
    

    Requirements

    • Use if-else structure
    • Handle both cases

    Input

    7

    Expected Output

    Odd

  7. Largest of Three Numbers
  8. Write a program to find the largest of three numbers.
    

    Requirements

    • Use if-elif-else structure
    • Handle all possible cases

    Input

    5
    9
    2
    

    Expected Output

    9

  9. Grade Assignment
  10. Write a program to assign grades based on the score:
    - A (90-100)
    - B (80-89)
    - C (70-79)
    - D (60-69)
    - F (<60)
    

    Requirements

    • Use if-elif-else structure
    • Handle edge cases

    Input

    85

    Expected Output

    B

  11. Age Group Classification
  12. Write a program to classify age into:
    - Child (0-12)
    - Teenager (13-19)
    - Adult (20-64)
    - Senior (65+)
    

    Requirements

    • Use if-elif-else structure
    • Handle all age ranges

    Input

    25

    Expected Output

    Adult

  13. Temperature Description
  14. Write a program to describe the temperature:
    - Hot (>30°C)
    - Warm (20-30°C)
    - Cold (<20°C)
    

    Requirements

    • Use if-elif-else structure
    • Handle all temperature ranges

    Input

    25.5

    Expected Output

    Warm

  15. Voter Eligibility
  16. Write a program to check if a person is eligible to vote (18+ years).
    

    Requirements

    • Use if-else structure
    • Handle edge case (exactly 18)

    Input

    17

    Expected Output

    Not eligible

  17. Password Strength Checker
  18. Write a program to check the strength of a password:
    - Weak (length <6)
    - Medium (6-10 characters)
    - Strong (>10 characters)
    

    Requirements

    • Use if-elif-else structure
    • Check only length for simplicity

    Input

    password123

    Expected Output

    Strong

  19. Day of the Week
  20. Write a program to print the day of the week based on a number (1-7).
    

    Requirements

    • Use if-elif-else structure
    • Handle invalid inputs

    Input

    3

    Expected Output

    Wednesday

  21. Discount Calculator
  22. Write a program to calculate the discount on a product:
    - 10% if price >=1000
    - 5% if price >=500
    - No discount otherwise
    

    Requirements

    • Use if-elif-else structure
    • Calculate final price after discount

    Input

    750

    Expected Output

    712.5

  23. Character Checker
  24. Write a program to check if a character is a vowel or consonant.
    

    Requirements

    • Use if-elif-else structure
    • Handle both uppercase and lowercase

    Input

    E

    Expected Output

    Vowel

  25. Valid Triangle Checker
  26. Write a program to check if three given lengths can form a triangle.
    (Sum of any two sides must be greater than the third)
    

    Requirements

    • Use if-else structure with logical operators
    • Check all three conditions

    Input

    3
    4
    5
    

    Expected Output

    Valid triangle

  27. Multiples of Five
  28. Write a program to check if a number is a multiple of five.
    

    Requirements

    • Use if-else structure
    • Handle both cases

    Input

    25

    Expected Output

    Multiple of five

  29. Alphabet Case Checker
  30. Write a program to check if an alphabet is uppercase or lowercase.
    

    Requirements

    • Use if-else structure
    • Handle non-alphabet inputs

    Input

    H

    Expected Output

    Uppercase

  31. Basic Grade Classifier
  32. Write a program that:
    1. Takes a numerical score as input (0-100)
    2. Prints the grade based on:
       - A (90-100)
       - B (80-89)
       - C (70-79)
       - D (60-69)
       - F (<60)
    

    Requirements

    • Use if-elif-else structure
    • Handle edge cases (scores exactly at boundaries)

    Input

    85

    Expected Output

    Grade: B

    📚 Python if statements


  33. Number Properties Checker
  34. Write a program that:
    1. Takes a number as input
    2. Prints whether the number is:
       - Positive, negative or zero
       - Even or odd (only if non-zero)
    

    Requirements

    • Use nested if statements
    • Handle zero case separately

    Input

    -7

    Expected Output

    Negative
    Odd
    

    📚 Python nested if


🟡 Intermediate Exercises

  1. Leap Year Check
  2. Write a program to check if a year is a leap year or not.
    A leap year is divisible by 4, but not by 100 unless also divisible by 400.
    

    Requirements

    • Use nested if-elif-else structure
    • Handle all leap year conditions

    Input

    2020

    Expected Output

    Leap year

  3. Simple Calculator
  4. Write a simple calculator program that performs:
    - Addition (+)
    - Subtraction (-)
    - Multiplication (*)
    - Division (/)
    

    Requirements

    • Use if-elif-else structure
    • Handle division by zero

    Input

    5
    2
    /
    

    Expected Output

    2.5

  5. BMI Calculator
  6. Write a program to calculate BMI and categorize:
    - Underweight (<18.5)
    - Normal (18.5-24.9)
    - Overweight (25-29.9)
    - Obese (30+)
    

    Requirements

    • Use if-elif-else structure
    • Calculate BMI as weight(kg)/height(m)^2

    Input

    1.75
    70
    

    Expected Output

    Normal

  7. Triangle Type Checker
  8. Write a program to check the type of a triangle:
    - Equilateral (all sides equal)
    - Isosceles (two sides equal)
    - Scalene (all sides different)
    

    Requirements

    • Use if-elif-else structure
    • First validate if sides can form a triangle

    Input

    5
    5
    5
    

    Expected Output

    Equilateral

  9. Month Days
  10. Write a program to print the number of days in a given month (1-12).
    Assume February has 28 days (ignore leap years).
    

    Requirements

    • Use if-elif-else structure
    • Handle invalid inputs

    Input

    4

    Expected Output

    30

  11. Login System Simulator
  12. Create a simple login system that:
    1. Takes username and password as input
    2. Checks:
       - If username is "admin" and password is "12345", print "Access granted"
       - If username is correct but password wrong, print "Wrong password"
       - If username doesn't exist, print "User not found"
    

    Requirements

    • Use if-elif-else with multiple conditions
    • Use string comparison

    Input

    admin
    12345
    

    Expected Output

    Access granted

    📚 Python logical operators


  13. Leap Year Calculator
  14. Write a program that:
    1. Takes a year as input
    2. Determines if it's a leap year (print "Leap year" or "Not a leap year")
       - Rules:
         - Divisible by 4 → leap year
         - Except if divisible by 100 → not leap year
         - Unless also divisible by 400 → leap year
    

    Requirements

    • Use nested if statements
    • Handle all leap year conditions

    Input

    2000

    Expected Output

    Leap year

    📚 Python condition precedence


  15. Shopping Discount Calculator
  16. Create a program that:
    1. Takes total purchase amount as input
    2. Applies discounts:
       - 5% if amount ≥ 1000
       - 10% if amount ≥ 5000
       - 15% if amount ≥ 10000
    3. Prints final amount after discount
    

    Requirements

    • Use if-elif-else structure
    • Calculate discount mathematically

    Input

    7500

    Expected Output

    6750.0

    📚 Python math operations


🔴 Advanced Exercises

  1. Triangle Type Checker
  2. Write a program that:
    1. Takes three side lengths (a, b, c) as input
    2. Determines if they form:
       - Equilateral triangle (all sides equal)
       - Isosceles triangle (exactly two sides equal)
       - Scalene triangle (no sides equal)
       - Not a triangle (sum of any two sides ≤ third side)
    

    Requirements

    • Use nested if statements
    • Check triangle validity first

    Input

    5
    5
    8
    

    Expected Output

    Isosceles triangle

    📚 Python comparison operators


🧠 Practice & Progress

Explore More Topics

📘 Learn Python

Tutorials, Roadmaps, Bootcamps & Visualization Projects

Python Fundamentals

Flow Control Statements


Python Functions


Fundamentals more ...




🧠 Python Advanced

Object-Oriented Programming in Python (OOP)

More...

🧠 Modules