Specialty Digest

Global English editionLearners’ Study Dashboard — in development
The Knowledge Library for All

Flashcards: open the module flashcards (end-of-module review with workplace questions).

Online Courses / University Track / Python from Zero

Module 2 · Lesson 1 · Core

Comparisons: questions with True or False answers

Use the six comparison operators and tell = from ==.

  • 55 minutes
  • Serves O3
  • Assumes Module 1

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.

Valuesage and 18Compareage >= 18AnswerTrue or False
A comparison asks a question and the answer is always True or False.
OperatorMeaningExampleResult
==equal to5 == 5True
!=not equal to5 != 5False
< >less than, greater than3 < 9True
<= >=at most, at least7 >= 7True

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
False

The 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).

  1. 12 != 12 asks whether 12 is not equal to 12. (1 mark)
  2. They are equal, so the answer is False. (1 mark)
  3. 9 <= 9 asks whether 9 is at most 9. It is, so True. (1 mark)
  4. Answer: False then True. (1 mark)

Go deeper: the professor's notes

A comparison is a yes-or-no question. If the question is never asked, the program trusts its input.
Ask

Compare two values.

Answer

True or False.

Skip

Never asking is the bug.

Valuesclaimed and realCompare==AnswerTrue or FalseActaccept or reject
Comparison

Operators

SymbolQuestion
==equal?
>=at least?

At work: A length check

A message says it is 10 characters long.

  1. Count the real characters.
  2. Compare with 10.
  3. 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:

2. To ask whether two values are equal, use:

3. What does print("Ann" == "ann") display?

4. What does print(7 >= 7) display?

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

MistakeWhy 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 numberConvert first, for example with int().

Limits. This lesson covers numbers and text; other value types appear in later modules.

Flashcards for this lesson (3)

Tap a card to reveal the answer. Read the question, answer it aloud or in writing first, then check yourself against the explanation.

Card 1 of 3 · What does a comparison give you?
ValuestwoCompareoperatorAnswerTrue or False
Comparison

A Boolean value, True or False. Comparing two values answers a yes-or-no question. At work, stock < 10 is the question that decides whether to reorder.

Card 2 of 3 · What is the difference between = and ==?
=store==askCheckif line
Store or ask

= stores a value in a variable; == asks whether two values are equal. Writing = in an if line gives a syntax error. Reviewers check this first in any decision code.

Card 3 of 3 · Why does "Ann" == "ann" give False?
Anncapital Aannsmall aResultFalse
Exact text

Python compares text exactly, so capital and small letters differ. When matching user input, convert both sides to the same case first, which is a common step in sign-in forms.

Review with flashcards

Test yourself on this module with workplace flashcards and detailed answers.

Open the module flashcards