Python · Lesson 3

Making Decisions

Last lesson your program could talk. Today it starts to think: comparing values and using if / else so it responds differently depending on what the user types.

1. One equals stores, two equals compares

This is the number-one beginner mix-up, so get it straight first:

  • = stores. age = 13 puts the value 13 into the variable age.
  • == compares. age == 13 asks a question, “is age equal to 13?”, and answers True or False.
age = 13        # change this with the control below
print(age == 13)  # is age equal to 13?
print(age == 10)  # is age equal to 10?
Read it out loud. = is “becomes”. == is “is equal to?”. They are not the same thing.

2. Comparison operators and Booleans

Every comparison gives back a Boolean, either True or False.

OperatorMeansExampleResult
==is equal to5 == 5True
!=is not equal to3 != 3False
>is greater than9 > 4True
<is less than9 < 4False
>=greater than or equal13 >= 13True
<=less than or equal12 <= 11False

Try reading each of these and deciding the answer before you check: is 5 == 5? is 3 != 3? is 9 > 4?

3. The if statement

An if runs the indented code only when its condition is True. Two things matter: the colon at the end of the if line, and the indentation (4 spaces) that marks the body.

age = 15
if age >= 13:
    print("You are old enough.")

If the condition is False, the indented line simply does not run, so nothing is printed.

4. if / else

else covers everything the if did not. Exactly one of the two blocks runs.

score = int(input("What did you score on the test? "))

if score >= 50:
    print("You passed! Well done.")
else:
    print("Not quite, have another go.")
Indentation marks the blocks. Python uses the spaces to know which lines belong to the if and which belong to the else.

5. Worked example: secret-word game

This pulls Lesson 1 (input()) and today’s lesson (if / else with ==) together.

secret = "python"
guess = input("Guess the secret word: ")

if guess == secret:
    print("Correct! You got it.")
else:
    print("Nope, try again.")

Notice we compare with == (a question), never = (which would try to store).

6. Quick check Part 1 of 2

Six quick questions. This is half of completing the module. The coding task below is the other half.

What does == do?

In age = 13, what does the single = do?

Is this condition True or False? 5 == 5

Is this condition True or False? 3 != 3

In an if / else, when does the else block run?

What must go at the end of an if line in Python?

7. Coding task: Old enough to sign up? Part 2 of 2

Write a real program below and press Check. Our app is for ages 13 and over. Read an age, then use if / else to either let them in or turn them away. Passing this is the other half of completing the module.

solution.py
Output

            

Ctrl+Enter runs your code · Tab inserts 4 spaces · Python loads the first time you press Run or Check (5-10 seconds), then it’s instant.

How the module is marked: the quick check is worth 50% and this coding task is worth 50%. Do both to reach 100%. Your teacher can see your score on the progress page.