Phase 1: Prepare
Step 1 Warm-up recall
From Module 1: what does input() always return?
Private: nothing you type here is sent or stored.
Step 2 Learning goal
By the end of this lesson you can:
- use the six comparison operators (Foundation);
- predict True or False for a comparison (Core);
- tell = from == (Core).
Phase 2: Teach
Step 3 A familiar example
A door lock asks one question: does the code entered equal the stored code? The answer is yes or no. A program asks the same kind of question with a comparison.
Step 4 Clear explanation
A comparison puts two values side by side and gives a Boolean value: True or False.
| Operator | Meaning | Example | Result |
|---|---|---|---|
| == | equal to | 5 == 5 | True |
| != | not equal to | 5 != 5 | False |
| < > | less than, greater than | 3 < 9 | True |
| <= >= | at most, at least | 7 >= 7 | True |
Be careful with two symbols. = stores a value (Module 1). == asks whether two values are equal. Writing = where you meant == is one of the most common beginner errors.
age = 20
print(age >= 18)
print(age == 18)
print("Ann" == "ann")Output:
True
False
FalseThe last line is False because Python compares text exactly, so capital and small letters differ.
Step 5 Worked example (4 marks)
Predict print(12 != 12) and print(9 <= 9).
- 12 != 12 asks whether 12 is not equal to 12. (1 mark)
- They are equal, so the answer is False. (1 mark)
- 9 <= 9 asks whether 9 is at most 9. It is, so True. (1 mark)
- Answer: False then True. (1 mark)
Go deeper: the professor's notes
Compare two values.
True or False.
Never asking is the bug.
Operators
| Symbol | Question |
|---|---|
| == | equal? |
| >= | at least? |
At work: A length check
A message says it is 10 characters long.
- Count the real characters.
- Compare with 10.
- Reject if not equal.
What a careful practitioner decides. Never trust a stated size; compare it with the real one.
Phase 3: Practise
Step 6 Learner practice (instant-marked)
1. The value of 5 > 8 is:
5 is not greater than 8.
2. To ask whether two values are equal, use:
== is the comparison.
3. What does print("Ann" == "ann") display?
Case matters.
4. What does print(7 >= 7) display?
At least 7 includes 7.
Phase 4: Close
Step 7 Recap
- A comparison gives True or False.
- == compares; = stores.
- Text comparison is exact, including capitals.
Step 8 Exit ticket (ungraded)
Predict the output of print(10 != 4) and print(3 == 3.0).
Private: nothing you type here is sent or stored.
Step 9 Go further (Extension)
Type five comparisons of your own into Python and check your predictions.
In industry: Heartbleed: a check that was never made
OpenSSL's heartbeat feature echoed back a message of a length the sender claimed. The code never checked that the claimed length matched the real message. An attacker could send a short message with a large requested length and read up to 64 KB of server memory each time. The bug was introduced in 2012 and made public in April 2014.
Link to this lesson. A comparison between a claimed value and a real value is a condition. If you never write it, the program trusts whatever it is given.
Source: OWASP, Heartbleed Bug; Invicti, The Heartbleed Bug; Wikipedia, Heartbleed
Common mistakes
| Mistake | Why it fails |
|---|---|
| Writing = when you mean == | One equals sign stores a value; two compare. |
| Forgetting that capital letters matter | "Ann" and "ann" are different. |
| Comparing text to a number | Convert first, for example with int(). |
Limits. This lesson covers numbers and text; other value types appear in later modules.