>> Module 01 / Day 06 / Lecture

if / elif / else, In Depth

Day 6 of a 40-day journey
You already know how to ask a question once. Today you learn to ask many questions in a row — and to combine them.
Lecture
75 min · 09:00–10:15
Lab
60 min · Grade Classifier + Leap Year
Module
M1 · Python Foundations
DAY 06 / 40
>> Today's roadmap

Six stops, then a lab

  1. Comparison operators — the questions that produce True / False
  2. Chained if / elif / else — pick exactly one branch
  3. Boolean operators — and, or, not
  4. Nested conditions — a question inside a question
  5. Putting it together + common mistakes
  6. Lab — Grade Classifier + Leap Year
>> The hook

Every useful program makes decisions.

Pass or fail. Leap year or not. Adult or minor. The computer can't judge — but it can compare, and it can branch. Today we turn a single if into a real decision tree.

first_decision.py
score = 85
if score >= 90:
    print("A")
elif score >= 80:
    print("B")   # <- this one wins
else:
    print("keep going")
>> 01 / 05 · The questions

Comparison operators make True / False

Every condition is really a yes/no question. These six operators are how you ask them.

comparisons.py
7 > 3     # True   greater than
3 >= 3    # True   greater or equal
2 < 9     # True   less than
5 == 5    # True   EQUAL (two ='s!)
5 != 6    # True   not equal
4 <= 2    # False  less or equal
One vs two equals.
= assigns a value. == asks "are these equal?". Mixing them up is the #1 beginner bug.
>> 02 / 05 · The chain

elif — pick exactly one branch

Python checks branches top to bottom. The first one that is True runs — the rest are skipped entirely.

grade.py
def classify(score):
    if score >= 90:
        return "A"
    elif score >= 80:
        return "B"
    elif score >= 70:
        return "C"
    else:
        return "F"
Order is everything.
Check the highest band first. Put >= 70 before >= 90 and every score becomes a "C".
>> 03 / 05 · Combining questions

and · or · not

and
True and True → True

Both sides must be true.

or
False or True → True

At least one side must be true.

not
not True → False

Flips true and false.

>> 04 / 05 · A question inside a question

Nested conditions

An if can live inside another if. Often you can flatten it with and — but nesting is fine when the inner question only makes sense after the outer one.

nested.py
if logged_in:
    if is_admin:
        print("Welcome, boss")
    else:
        print("Welcome, user")
else:
    print("Please log in")
flattened.py
# Same idea, flattened with 'and'
if logged_in and is_admin:
    print("Welcome, boss")
elif logged_in:
    print("Welcome, user")
else:
    print("Please log in")
>> 05 / 05 · Putting it together

The leap-year rule, in one line

Divisible by 4, and (not divisible by 100 or divisible by 400). All three operators at once.

leap_year.py
def is_leap_year(year):
    return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)

print(is_leap_year(2000))   # True  (÷400)
print(is_leap_year(1900))   # False (÷100 but not ÷400)
print(is_leap_year(2024))   # True
>> Common failures

Five mistakes you'll make today

  1. Using = instead of == inside an if — Python raises a SyntaxError.
  2. Wrong order in the chain — checking >= 70 before >= 90, so an A becomes a C.
  3. Writing if x == 1 or 2 — you mean x == 1 or x == 2.
  4. A wall of separate ifs when you meant elif — several branches all fire.
  5. Forgetting the : at the end of the line, or mis-indenting the body.
>> 11:00 – 12:00 · Lab

Today's lab — Grade Classifier + Leap Year

PROBLEM 1
Grade Classifier

classify_grade(score) returns A / B / C / D / F using a chained if / elif / else.

PROBLEM 2
Leap Year

is_leap_year(year) returns True / False — a showcase for and / or / not.

>> End of Day 06

What you can now do

  • ✓ Build a multi-way decision with chained if / elif / else.
  • ✓ Combine conditions with and, or, and not.
  • ✓ Nest conditions — and know when to flatten them instead.
01 / 11
←/→ navigate   F fullscreen   Ctrl/⌘+P save as PDF