ShareYourGroupShareYourGroupPython

Lesson 8 of 21 · about 7 minutes with the exercises

If, elif and else

Until now your programs ran every line, in order, every time. A useful program makes choices. It gives a discount only above a certain bill, or prints "pass" only when the marks are high enough. The if statement is how Python chooses.

Comparisons give True or False

A comparison asks a yes or no question about two values. The answer is a bool: True or False.

SignQuestion
==are they equal?
!=are they different?
>is the left one greater?
<is the left one smaller?
>=greater or equal?
<=smaller or equal?
marks = 72
print(marks >= 33)
print(marks == 100)
print(marks != 72)
print("apple" == "Apple")

Output

True
False
False
False

Notice the double equals sign. A single = stores a value. A double == compares two values. Mixing them up is the most common beginner mistake in this lesson.

The if statement

marks = 72
if marks >= 33:
    print("Pass")
    print("Well done")
print("Result printed")

Output

Pass
Well done
Result printed

Three things make an if:

  1. The word if, then a condition.
  2. A colon at the end of the line.
  3. One or more lines indented underneath, by four spaces.

The indented lines form a block. They run only when the condition is True. The last line is not indented, so it is outside the block and runs every time.

Indentation is not decoration in Python. It is how the language knows which lines belong to the if. Most editors add the four spaces for you after a colon. A missing or uneven indent gives an IndentationError.

else: the other road

else gives a block to run when the condition is False. It has no condition of its own.

age = 15
if age >= 18:
    print("You can vote.")
else:
    print("You cannot vote yet.")

Output

You cannot vote yet.

Exactly one of the two blocks runs. Never both, never neither.

elif: more than two roads

elif is short for "else if". Python checks the conditions from the top and runs the block of the first one that is true. Then it skips the rest.

marks = 68
if marks >= 90:
    grade = "A"
elif marks >= 75:
    grade = "B"
elif marks >= 60:
    grade = "C"
elif marks >= 33:
    grade = "D"
else:
    grade = "Fail"
print("Grade:", grade)

Output

Grade: C

Order matters. 68 is also greater than 33, but the >= 60 test comes first, so the grade is C. If you wrote the >= 33 test at the top, everyone who passed would get a D.

Joining conditions

WordTrue when
andboth sides are true
orat least one side is true
notthe condition is false
age = 25
has_licence = True
if age >= 18 and has_licence:
    print("May drive")

day = "Sunday"
if day == "Saturday" or day == "Sunday":
    print("Weekend")

raining = False
if not raining:
    print("Go for a walk")

Output

May drive
Weekend
Go for a walk

A common slip is to write if day == "Saturday" or "Sunday":. That does not do what it seems to. Each side of or must be a full comparison.

Python also lets you chain comparisons the way you write them in maths.

temperature = 28
if 20 <= temperature <= 30:
    print("Pleasant weather")

Output

Pleasant weather

An if inside an if

A block can contain another if. Indent the inner block by four more spaces.

marks = 95
if marks >= 33:
    print("Pass")
    if marks >= 90:
        print("With distinction")

Output

Pass
With distinction

Two levels are fine. If you find yourself four levels deep, and or elif will usually make the code flatter and easier to read.

Truthy and falsy values

A condition does not have to be a comparison. Python treats some values as false: the number 0, an empty string "", an empty list and the special value None. Everything else counts as true. This gives a short way to ask "did the user type anything?".

name = ""
if name:
    print("Hello,", name)
else:
    print("You did not enter a name.")

Output

You did not enter a name.

Try it yourself

Exercise 1. A number is stored in n. Print "even" or "odd". Test with 17.

Show one answer
n = 17
if n % 2 == 0:
    print("even")
else:
    print("odd")

Output

odd

Exercise 2. A cinema charges 100 rupees for children under 12, 150 for people of 60 and above, and 200 for everyone else. Print the price for age = 64.

Show one answer
age = 64
if age < 12:
    price = 100
elif age >= 60:
    price = 150
else:
    price = 200
print("Ticket:", price)

Output

Ticket: 150

Exercise 3. A year is a leap year if it divides by 4, except that century years must divide by 400. Print whether 1900 is a leap year.

Show one answer
year = 1900
if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
    print(year, "is a leap year")
else:
    print(year, "is not a leap year")

Output

1900 is not a leap year

Examples on this page were run with Python 3.14 on 12 November 2026. The output shown is the real output.

This is an independent learning site. It is not connected with or approved by the Python Software Foundation. "Python" is a trademark of the Python Software Foundation and is used here only to name the language.