Python Learning Series · @onlysamarrr · Day 9 / 30

Day 9: elif and Nested if

Day 9 covers elif chains and nested if. Use elif when conditions must be checked one after another, and prefer and over a nested if when two conditions must both be true.

Phase: Beginner · Reels: 41/450 – 45/450

Quick Answer

Day 9 covers elif chains and nested if. Use elif when conditions must be checked one after another, and prefer and over a nested if when two conditions must both be true.

What You Will Learn

Code

marks = 85
if marks >= 90:
    print("A")
elif marks >= 75:
    print("B")
elif marks >= 60:
    print("C")
else:
    print("Fail")
age = 20
has_id = True
if age > 18 and has_id:
    print("Entry allowed")
else:
    print("Entry denied")

Output

B
Entry allowed

Code Explained, Line by Line

  1. In an elif chain only the first True condition's block runs; the rest are skipped.
  2. For marks = 85, the 90+ check fails and the 75+ check passes, so the grade is B.
  3. age > 18 and has_id requires both conditions to be True for entry to be allowed.
  4. Replacing a nested if with and makes code shorter and easier to read.
Common Mistake: Multiple if and elif are not the same: every multiple if is checked separately, while an elif chain stops at the first True condition.

Real-Life Use

Grade calculators, ticket pricing and access rules use elif chains in real apps.

Practice Task: Write a leap year check: a year is a leap year if it divides by 4 but not by 100, unless it also divides by 400.

Test Yourself — MCQs

Select (tick) an option, then tap Check answer to see instantly whether your answer is right or wrong, with an explanation. You can change your answer and check again.

Q1. marks = 85 with elif checks 90+, 75+, 60+. What prints?

Q2. Which year is a leap year?

Q3. What does age > 18 and has_id need?

Today's 5 Reels (from the 90-Day Plan Excel)

41/450Conceptelif kab use karein
42/450Live demoGrade calculator demo
43/450Common mistakeMultiple if vs elif
44/450QuizQuiz: leap year
45/450Interview / real lifeInterview: nested if vs and
← Day 8Day 10 →