Learn with Yasir

Share Your Feedback

Python if‑elif‑else Review Questions – Test Your Understanding

Review your knowledge of Python if‑elif‑else conditional statements with structured questions. Great for beginners preparing for exams, quizzes, or coding interviews.

🔍 Review Questions

  1. This code incorrectly classifies some leap years (e.g., 2000). Identify the flaw and fix the logic.

  2. def is_leap_year(year):  
      if year % 4 == 0:  
        return True  
      elif year % 100 == 0:  
        return False  
      elif year % 400 == 0:  
        return True  
      else:  
        return False  
    

  3. What is the purpose of the 'if' statement in Python?

  4. 💬 Answer

    The 'if' statement is used for conditional execution. It evaluates a condition and executes its block of code only if the condition is True.

    💡 Examples:

    ["Basic if: `if x > 0: print('Positive')`", "With variable: `if logged_in: print('Welcome')`"]

    📘 Related Topics:


  5. When should you use 'elif' instead of separate 'if' statements?

  6. 💬 Answer

    'elif' should be used when checking multiple exclusive conditions where only one block should execute. Separate 'if' statements check independently and multiple blocks could execute.

    💡 Examples:

    ["Good (exclusive): `if grade >= 90: ... elif grade >= 80: ...`", "Bad (non-exclusive): `if x > 0: ... if x < 10: ...` (both could run)"]

    📘 Related Topics:


  7. What is the purpose of the 'else' clause in conditionals?

  8. 💬 Answer

    The 'else' clause provides a default block of code that executes when all preceding 'if' and 'elif' conditions evaluate to False.

    💡 Examples:

    ["Basic else: `if x > 0: ... else: print('Not positive')`", "After elif: `if x > 0: ... elif x < 0: ... else: print('Zero')`"]

    📘 Related Topics:


  9. What is a nested conditional and when would you use one?

  10. 💬 Answer

    A nested conditional is an 'if' statement inside another 'if' statement. It's used when you need to check additional conditions only after a primary condition is met.

    💡 Examples:

    ["Basic nesting: `if x > 0: if x % 2 == 0: print('Positive even')`", "Login system: `if user_exists: if password_correct: ...`"]

    📘 Related Topics:


  11. How does Python determine which block to execute in an if-elif-else chain?

  12. 💬 Answer

    Python evaluates conditions from top to bottom. It executes the first block where the condition is True, skips all remaining conditions, and executes the 'else' block only if all conditions were False.

    💡 Examples:

    ["Order matters: `if x > 10: ... elif x > 5: ...` (second won't run if x=12)", "Only one executes: `if True: ... elif True: ...` (second never reached)"]

    📘 Related Topics:


  13. What are some common mistakes beginners make with Python conditionals?

  14. 💬 Answer

    Common mistakes include:
    1. Forgetting colons after conditions
    2. Using assignment (=) instead of comparison (==)
    3. Incorrect indentation of blocks
    4. Over-nesting conditionals unnecessarily
    5. Not covering all possible cases
    

    💡 Examples:

    ["Syntax error: `if x > 0 print('Positive')` (missing colon)", "Logical error: `if x = 5: ...` (assignment not comparison)"]

    📘 Related Topics:


  15. How can you test multiple conditions in a single 'if' statement?

  16. 💬 Answer

    Use logical operators: 'and' (both true), 'or' (either true), 'not' (inverse). Parentheses can group conditions for clarity and proper evaluation order.

    💡 Examples:

    ["AND: `if x > 0 and x < 10: ...`", "OR: `if x == 'yes' or x == 'y': ...`", "Combined: `if (x > 0 or y > 0) and not z: ...`"]

    📘 Related Topics:


🧠 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