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


🟡 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







🔴 Advanced Exercises