Overview
if/elif/else lets your program choose different paths based on conditions.
Analogy
A traffic light: green → go, yellow → slow, red → stop.
Step-by-step
- Write condition after if: if score >= 90:
- Add elif branches for additional conditions.
- End with else for the default case.
- Python checks top-to-bottom, takes the first True branch.
Visual
score=85 → if(>=90): False → elif(>=70): True → print('B')
Common mistakes
- Using = (assignment) instead of == (comparison).
- Overlapping elif conditions making some branches unreachable.
Practice questions
- Write a grade calculator (A/B/C/D/F).
- Check if a number is positive, negative, or zero.