runtime
12 / 250
lesson 12 Operators & Expressions · beginner

Compare two values

A comparison asks a question and answers True or False.

You'll be able to
  • Compare numbers and read True or False
  • Compare text, including capitals
  • Tell = (store) from == (compare), and < from <=
The idea

Programs ask questions all the time: is this customer old enough, is the cart over the free-shipping line, is the status delivered. == asks whether two values are the same. != asks whether they are different. <, >, <=, and >= compare order. The answer is True or False, the values from the previous module.

= stores a value. == compares. They are different.

Run each example to see what it prints. Running examples doesn't count toward progress.

example-1.py · Questions about numbers
print(3 < 10)
print(3 == 10)
print(3 != 10)
print(10 >= 10)
example-2.py · Questions about text
status = "delivered"
print(status == "delivered")
print(status == "Delivered")

The boundary matters. "Free shipping from 50" means a total of exactly 50 qualifies, so the test is total >= 50, not total > 50. Decide which side of the line the boundary value is on, then pick < or <=. A comparison is a value like any other: you can store it, adult = age >= 18, and print it later.

Review
  • ==, !=, <, >, <=, and >= answer True or False.
  • = stores a value. == compares two values. Decide whether the boundary value counts, then choose < or <=.
  • Text comparison cares about capitals. Next: combining two answers with and, or, and not.
Practise this

Loading your progress…

To master this lesson, solve both challenges without hints or the answer, and pass the concept check.

Compare two values — Operators & Expressions · Runtime