Review your knowledge of Python if‑elif‑else conditional statements with structured questions. Great for beginners preparing for exams, quizzes, or coding interviews.
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
The 'if' statement is used for conditional execution. It evaluates a condition and executes its block of code only if the condition is True.
["Basic if: `if x > 0: print('Positive')`", "With variable: `if logged_in: print('Welcome')`"]
'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.
["Good (exclusive): `if grade >= 90: ... elif grade >= 80: ...`", "Bad (non-exclusive): `if x > 0: ... if x < 10: ...` (both could run)"]
The 'else' clause provides a default block of code that executes when all preceding 'if' and 'elif' conditions evaluate to False.
["Basic else: `if x > 0: ... else: print('Not positive')`", "After elif: `if x > 0: ... elif x < 0: ... else: print('Zero')`"]
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.
["Basic nesting: `if x > 0: if x % 2 == 0: print('Positive even')`", "Login system: `if user_exists: if password_correct: ...`"]
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.
["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)"]
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
["Syntax error: `if x > 0 print('Positive')` (missing colon)", "Logical error: `if x = 5: ...` (assignment not comparison)"]
Use logical operators: 'and' (both true), 'or' (either true), 'not' (inverse). Parentheses can group conditions for clarity and proper evaluation order.
["AND: `if x > 0 and x < 10: ...`", "OR: `if x == 'yes' or x == 'y': ...`", "Combined: `if (x > 0 or y > 0) and not z: ...`"]
Tutorials, Roadmaps, Bootcamps & Visualization Projects