True, False and comparisons

Every decision a program makes starts with a yes-or-no question. Compare values with == != < > <= >=, and combine answers with and, or and not.

Lesson · 12 min

In this lesson you will

  • Use the six comparison operators and predict their True or False result
  • Tell = (assignment) apart from == (comparison)
  • Combine conditions with and, or and not
  • Store the result of a comparison in a variable

Up to now, every program you’ve written did the same thing every time: line 1, line 2, line 3, done. Real programs react. A game checks whether you’ve run out of lives; a shop checks whether you’ve got enough money; a thermostat checks whether the room is too cold. Every one of those checks is a yes-or-no question, and in Python the answer is a bool: True or False. You met bools briefly in the lesson on values and types. Now they take centre stage.

Asking questions with comparisons

A comparison compares two values and gives back True or False. Python has six:

OperatorQuestion it asksExampleResult
==equal to?5 == 5True
!=not equal to?5 != 5False
<less than?3 < 8True
>greater than?3 > 8False
<=less than or equal to?8 <= 8True
>=greater than or equal to?7 >= 8False

Predict each line, then run:

print(10 == 10)
print(10 != 3)
print(4 > 9)
print(4 <= 4)
age = 15
print(age >= 13)

The most common mistake: = versus ==

One equals sign and two equals signs mean completely different things:

  • x = 5 is assignment: “x now refers to 5”. It’s an instruction.
  • x == 5 is a comparison: “is x equal to 5?”. It’s a question, and its answer is a bool.
x = 5
print(x == 5)
print(x == 6)

If you accidentally write = where a question belongs, Python usually stops with a SyntaxError and often suggests “Maybe you meant ’==’ instead of ’=’?”. Read the hint; it’s nearly always right.

Comparisons can be stored

A comparison is an expression like any other, so you can store its answer in a variable. Names that read like yes-or-no questions work well:

temperature = 31
is_hot = temperature > 30
print(is_hot)
print(type(is_hot))

Comparing text

== works on strings too. Two strings are equal only if every character matches exactly, including capital letters and spaces:

password = "Banana"
print(password == "Banana")
print(password == "banana")
print("cat" != "dog")

< and > also work on strings: they compare in dictionary order, roughly, so "apple" < "banana" is True. But careful: all capital letters count as “smaller” than all lowercase letters, so "Zebra" < "apple" is also True. Comparing a string with a number using < is a TypeError. And == between a string and a number is simply False: "7" == 7 is False, because text is never equal to a number.

Combining questions: and, or, not

Real decisions often depend on more than one thing. To ride a roller coaster you might need to be tall enough and old enough. Python has three words for combining bools:

  • a and b is True only if both are True.
  • a or b is True if at least one is True.
  • not a flips it: True becomes False and False becomes True.
height = 135
age = 9
tall_enough = height >= 120
old_enough = age >= 10
print(tall_enough and old_enough)
print(tall_enough or old_enough)
print(not old_enough)

Change age to 10 and run again. Which answers change?

Here’s the whole of and and or in one table:

aba and ba or b
TrueTrueTrueTrue
TrueFalseFalseTrue
FalseTrueFalseTrue
FalseFalseFalseFalse

In everyday English “or” sometimes means “one or the other but not both”. In Python, or is True when both are True too.

Ranges in one go

Is a number between two others? You could write x >= 1 and x <= 10. Python also lets you chain comparisons the way you would in maths:

score = 7
print(1 <= score <= 10)
print(0 < score < 5)

Precedence, and when to use brackets

Comparisons are worked out before not, not before and, and and before or. That means a or b and c is read as a or (b and c). Rather than memorising this, use brackets whenever you mix and and or; they make your meaning clear to Python and to people.

is_weekend = True
is_holiday = False
raining = True
go_to_park = (is_weekend or is_holiday) and not raining
print(go_to_park)

Your turn

Exercise · Python

Can I ride?

The Thunder Loop roller coaster has two rules: riders must be at least 120 cm tall, and at least 8 years old, unless they ride with an adult, in which case only the height rule applies. Using the variables at the top, create bools (no if needed, just comparisons and and/or/not):

  • tall_enough: height is at least 120,
  • old_enough: age is at least 8,
  • can_ride: tall enough, and either old enough or with an adult.

Build the answers from the variables rather than typing True or False, then test your code by changing height, age and with_adult and checking the answers make sense.

height = 125
age = 7
with_adult = True

tall_enough = True
old_enough = True
can_ride = True
print(tall_enough, old_enough, can_ride)

Check yourself

Self-check · 4 questions

True, False and comparisons

1. What is the difference between = and ==?
2. What is True and False?
3. What is "Cat" == "cat"?
4. If x = 7, what is 1 <= x <= 5?