Definition
📌 Conditions
Python conditionals use if, elif (short for "else if"), and else — with indentation, not curly braces, marking each block..
Syntax
syntax
if condition:
# code
elif condition2:
# code
else:
# codeExample
py
marks = 72
if marks >= 90:
print("Grade: A")
elif marks >= 60:
print("Grade: B")
else:
print("Grade: C")Output
outputGrade: B
Important Points
- Indentation must be consistent (commonly 4 spaces) — mismatched indentation is a syntax error.
elifreplaces "else if" from other languages — spelled as one word.- A colon
:is required at the end of everyif/elif/elseline. - Python 3.10+ introduced
match/case, similar to a switch statement.
Common Mistakes
- Forgetting the colon
:at the end of the condition line. - Mixing tabs and spaces for indentation within the same block.
- Writing
else if(two words) instead of Python's single-wordelif.
Interview Tip
🎯 Before your interview
Be ready to explain why Python enforces indentation strictly — it removes ambiguity about which block a line belongs to, at the cost of being whitespace-sensitive.
