Lesson 8 of 12

If / Elif / Else ❓

🎯 Grades 6–8 ⏱ ~25 minutes 💚 Intermediate

What You'll Learn

  • Write if/elif/else statements in Python
  • Use comparison operators
  • Build a grade checker program

If Statements

An if statement runs code only when a condition is True:

Python
temperature = 95
if temperature > 90:
    print('It is hot outside!')
print('This always prints')
💡
Indentation Matters!

Python uses indentation (4 spaces or a Tab) to know which code belongs inside the if block. Get this wrong and your code will break!

Elif and Else

Python
score = 85
if score >= 90:
    print('A - Excellent!')
elif score >= 80:
    print('B - Great job!')
elif score >= 70:
    print('C - Good effort!')
else:
    print('Keep studying!')

Comparison Operators

Python
x = 10
print(x == 10)  # True  (equal to)
print(x != 5)   # True  (not equal)
print(x > 8)    # True  (greater than)
print(x < 5)    # False (less than)
print(x >= 10)  # True  (greater or equal)
print(x <= 10)  # True  (less or equal)
🎓">Grade Checker

Ask the user for a test score (0-100). Print their letter grade and a motivating message using if/elif/else.

Quick Check

How many spaces are used for indentation in Python?

A2 spaces
B8 spaces
C4 spaces (or 1 Tab)

What does == mean in Python?

AAssign a value
BCheck if two things are equal
CGreater than or equal

Which runs when all if and elif conditions are False?

Aelif
Belse
Cthen