Python / Making decisions

The if statement

Make a program run commands only when a condition holds, using if, indentation and the == comparison. Baccalaureate

So far a program runs every line in order, always. But sometimes it must decide: "if the student read 100 books, give a badge". That is a condition.

Python
books = 100
if books == 100:
    print("Reading Champion Badge!")
Console
Reading Champion Badge!
PartMeaningIn the example
After ifThe conditionbooks == 100
End of the lineA required colon:
The indented line belowWhat runs if the condition holdsprint("Reading Champion Badge!")

A condition is True or False

== asks: are the two values equal? The answer is one of two values: True or False. You can print it:

Python
books = 100
print(books == 100)
books = 80
print(books == 100)
Console
True
False

An if runs what is under it only when the condition is True. When it is False, it skips it as if it were not there.

= assigns (put the value in); == compares (are they equal?). Writing if books = 100: is an error.

Indentation

Python knows which lines belong to the if by their indentation: 4 spaces at the start of the line. A line that is not indented does not belong to the if, so it always runs:

Python
books = 80
if books == 100:
    print("Reading Champion Badge!")
    print("Well done!")
print("See you next week")
Console
See you next week

Forgetting the indentation causes an error:

Python
books = 100
if books == 100:
print("Reading Champion Badge!")
Console
IndentationError: expected an indented block after 'if' statement on line 2
The two most common mistakes: forgetting the colon : at the end of the if line, and forgetting to indent the next line.

Practice

1Fill in the blankCurriculum practice

Complete the condition line: if books equals 100, print Reading Champion Badge!

Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
books = 100
___
    print("Reading Champion Badge!")
Console
Press Run to see the result.
2Write the programCurriculum practice

Assign 100 to english; if english equals 100, print Get a reward.

Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.
3Write the programCurriculum practice

Assign 100 to num; if num equals 100, print Correct.

Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.
4Write the programCurriculum practice

Assign 80 to math, then print the result of the comparison math == 80 (it should show True).

Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.
5Edit the programExtra practice

The program has two mistakes that stop it running. Fix them so it prints Free entry then Enjoy the museum.

HintCheck the comparison sign and the end of the if line.
Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
age = 6
if age = 6
    print("Free entry")
print("Enjoy the museum")
Console
Press Run to see the result.
6Write the programExtra practice

Assign "nile2026" to password. If password equals "nile2026", print Welcome back. At the end (outside the if) always print Goodbye.

Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.

Key points

  • Shape: if condition:, then the commands that belong to it on the next lines, indented 4 spaces.
  • A condition is True or False; if runs its block only when it is True.
  • = assigns; == compares.