If Statements: Making Decisions

What you’ll learn in this lesson

  • How if runs code only when a condition is true
  • How else covers the “otherwise” case
  • How elif chains several choices
  • Why indentation is part of the code, not decoration
  • The two most common if mistakes

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 7.

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

  • Forgetting the colon: if age >= 10 without : gives a syntax
    error. Every if, elif, and else line ends with a colon.
  • Mixing indentation: some lines indented two spaces and others
    four, or tabs mixed with spaces, gives an indentation error. Pick
    four spaces and use them everywhere. IDLE helps: pressing Tab in
    IDLE inserts spaces for you.

If Statements: Making Decisions

Python for Absolute Beginners · Lesson 9

What you’ll learn

  • How if runs code only when a condition is true
  • How else covers the “otherwise” case
  • How elif chains several choices
  • Why indentation is part of the code, not decoration
  • The two most common if mistakes
1 / 14

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
  • The if line ends with a colon (:) — “here comes the code for this case”
  • The lines underneath are indented (usually four spaces) — that is how Python knows they belong to the if
2 / 14

A first if statement

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

Output:
You are 10 or older.
Done.

The final print is not indented, so it runs no matter what.

3 / 14

The same program, age 8

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.

4 / 14

Otherwise: else

  • else covers the case where the condition is false
  • else needs no condition of its own — it means “in every other case”
  • Like if, it ends with a colon and its code is indented
5 / 14

Else in code

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

Output:
Keep practicing — you will get there.

The score is below 50, so the else block runs.

6 / 14

Several choices: elif

  • elif (short for “else if”) chains more than two cases in order
  • Python checks each condition from the top and runs the first block whose condition is true — and only that one
  • Order matters: always put the strictest condition first, or a looser one above it will swallow every case below
7 / 14

Elif in code

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!

85 is not >= 90, so Python moves on; it is >= 70, so it prints “Good job!” and stops checking.

8 / 14

Indentation is not decoration

  • In most languages, indentation is just tidiness. In Python, it is meaning
  • Same lines, different indentation, different program
  • With age 8, a program where both greeting lines are indented prints only “Goodbye.”
  • With age 8, a program where only the first line is indented still prints “Welcome in!”
  • Indent every line that belongs to the if, elif, or else above it — four spaces is the standard
9 / 14

The two most common if mistakes

  • Forgetting the colon: an if line without : gives a syntax error — every if, elif, and else line ends with a colon
  • Mixing indentation: some lines two spaces and others four, or tabs mixed with spaces, gives an indentation error
  • Pick four spaces and use them everywhere — in IDLE, pressing Tab inserts spaces for you
10 / 14

Hands-on exercise

  • 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
  • Extend it with elif: under 13 → “Child”, 13 to 19 → “Teen”, 20 or older → “Adult” — test it with three different ages
  • Deliberately remove the colon from an if line, read the error, then put the colon back and confirm the program works
11 / 14

Key takeaways

  • if runs its indented block only when its condition is true
  • else covers every other case, with no condition of its own
  • elif chains choices in order; the first true condition wins, and only it runs
  • Every if / elif / else line ends with a colon; its block is indented four spaces
  • Indentation is meaning in Python — same lines, different indent, different program
12 / 14

Take the quiz

Take the Lesson 9 quiz — 5 quick questions to check what you learned.

13 / 14

Next lesson

Loops: Repeating Without Retyping

14 / 14

Press play on any slide and the lesson plays on by itself from there. You can still click Next or click a slide to move on.

Download the Lesson 9 slides (PowerPoint, with narration)

Download the Lesson 9 slides (PDF)

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.

Lesson 09: If Statements: Making Decisions

The program picks one message depending on the score, using if / elif / else from the lesson. Change the score and run again.

Loading Python… this can take a few seconds the first time. Please wait — the Run button will become available when Python is ready.

Try this: Change the score to 85 and press Run again — the message becomes “Good job!” Try 95 and 60 too.

The code that runs



Output

Press Run to see the output here.

Practice file

Save this as lesson09-practice.py and run it with F5 in IDLE.

# 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")

Key takeaways

  • if runs its indented block only when its condition is true.
  • else covers every other case, with no condition of its own.
  • elif chains choices in order; the first true condition wins, and only it runs.
  • Every if/elif/else line ends with a colon; its block is indented four spaces.
  • Indentation is meaning in Python — same lines, different indent, different program.

Next lesson: Loops.


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. One — the first true block (and the else, if none is). 5. Because indentation is how Python decides which lines belong to an if, elif, or else.