Lesson 9: If Statements: Making Decisions


What you'll learn in this lesson


The first decision

Everything so far has run straight from top to bottom. An if statement breaks that: it runs some code only when a condition is true, and skips it otherwise. The condition is just a comparison, like the ones from Lesson 8.

age = 12
if age >= 10:
    print("You are 10 or older.")
print("Done.")

Output:

You are 10 or older.
Done.

Three things to notice. First, the line ends with a colon (:) — it says "here comes the code for this case." Second, the print underneath is indented (pushed in, usually four spaces). The indent is how Python knows that line belongs to the if. Third, the final print("Done.") is not indented, so it runs no matter what.

Change age to 8 and run it again:

age = 8
if age >= 10:
    print("You are 10 or older.")
print("Done.")

Output:

Done.

The indented line is skipped entirely, because the condition is False. The unindented line still runs. That is the whole mechanism: condition true → run the indented block; condition false → skip it.


Otherwise: else

else covers the case where the condition is false:

score = 45
if score >= 50:
    print("You passed!")
else:
    print("Keep practicing — you will get there.")

Output:

Keep practicing — you will get there.

else needs no condition of its own — it means "in every other case." And like if, it ends with a colon and its code is indented.


Several choices: elif

When there are more than two cases, elif (short for "else if") chains them in order. Python checks each condition from the top and runs the first block whose condition is true — and only that one:

score = 85
if score >= 90:
    print("Excellent!")
elif score >= 70:
    print("Good job!")
elif score >= 50:
    print("You passed.")
else:
    print("Keep practicing.")

Output:

Good job!

score >= 90 is false, so Python moves on. score >= 70 is true, so it prints "Good job!" and stops checking — it does not matter that score >= 50 would also have been true. Order matters: always put the strictest condition first, or a looser one above it will swallow every case below.


Indentation is not decoration

In most languages, indentation is just tidiness. In Python, it is meaning. These two programs do different things:

age = 15
if age >= 10:
    print("Old enough.")
    print("Welcome in!")
print("Goodbye.")

Output:

Old enough.
Welcome in!
Goodbye.
age = 15
if age >= 10:
    print("Old enough.")
print("Welcome in!")
print("Goodbye.")

Output:

Old enough.
Welcome in!
Goodbye.

Wait — those outputs are the same. Now try age = 8 in each. In the first, both indented lines are skipped, so only "Goodbye." prints. In the second, only "Old enough." is skipped, and "Welcome in!" still prints. Same lines, different indentation, different program. Always indent every line that belongs to the if, elif, or else above it — and keep the indent consistent (four spaces is the standard).


The two most common if mistakes


Hands-on exercise

  1. Write a program that stores your age and prints "You are a teen" if it is 13 or older, and "You are not a teen yet" otherwise.
  2. Extend it with elif: under 13 → "Child", 13 to 19 → "Teen", 20 or older → "Adult". Test it with three different ages.
  3. Deliberately remove the colon from an if line, read the error, then put the colon back and confirm the program works.

Quick quiz

  1. What does an if statement do?
  2. What two things mark the code that belongs to an if?
  3. What does else mean, and does it take a condition?
  4. In an if/elif/else chain, how many blocks run?
  5. Why does indentation matter in Python in a way it does not in most other languages?

Answer key: 1. It runs code only when its condition is true, and skips it otherwise. 2. A colon at the end of the if line, and indentation on the lines below. 3. "In every other case" — it takes no condition of its own. 4. At most one: the first block whose condition is true (or the else, if none is). 5. Because indentation is how Python decides which lines belong to an if, elif, or else.


Key takeaways

Next lesson: Loops.


Slides

Work through the slides with Helio's narration, or download them:

Open the slidesDownload PowerPointDownload PDF

Try it yourself

Run the lesson's example with real Python, right in your browser (Python loads from the internet, so you need to be online).

Open the demo

Practice file

Download lesson09-practice.pyDownload the solution

# Lesson 9: If Statements — Making Decisions
# WHAT TO DO: An if statement runs its indented lines only when its
# condition is true. Age 15 should count as a teen (13 or older), but
# one line is wrong. Fix line 10: change 20 to 13 in the if condition.
# HINT: Only change the number 20. The indented print lines are already
# right, and else covers every other case.
# Run with F5 in IDLE. You are done when it prints "You are a teen".

age = 15
if age >= 20:  # <-- CHANGE THIS LINE: a teen is 13 or older
    print("You are a teen")
else:
    print("You are not a teen yet")

Save the file as lesson09-practice.py and run it with F5 in IDLE. On a Mac, if F5 does nothing, use Run, then Run Module, from the menu.

Check yourself

Five quick questions on this lesson. Take the Lesson 9 quiz

Want graded quizzes and progress tracking?

Enroll free in the full course — same 17 lessons, with your progress saved. Free, no payment.