If Statements: Making Decisions

Python for Absolute Beginners · Lesson 9

What you’ll learn

1 / 14

The first decision

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

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

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

9 / 14

The two most common if mistakes

10 / 14

Hands-on exercise

11 / 14

Key takeaways

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.