1. One equals stores, two equals compares
This is the number-one beginner mix-up, so get it straight first:
=stores.age = 13puts the value13into the variableage.==compares.age == 13asks a question, “is age equal to 13?”, and answersTrueorFalse.
age = 13 # change this with the control below print(age == 13) # is age equal to 13? print(age == 10) # is age equal to 10?
= 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.
| Operator | Means | Example | Result |
|---|---|---|---|
== | is equal to | 5 == 5 | True |
!= | is not equal to | 3 != 3 | False |
> | is greater than | 9 > 4 | True |
< | is less than | 9 < 4 | False |
>= | greater than or equal | 13 >= 13 | True |
<= | less than or equal | 12 <= 11 | False |
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.")
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.
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.