LESSON 08

Booleans & Comparisons

Python learns to answer yes or no.

USES: print( ), variables, strings and .lower( ), numbers, input( ), int( ), type( ).

01

Yes or no

So far every value has been text or a number. There is a third kind with only two possible values: True and False. It is called a boolean, bool for short. Two rules: capital first letter, because true does not work; and no quotes, because "True" is just text that spells the word.

INPUTprint(True) print(False) print(type(True))
OUTPUTTrue False <class 'bool'>
TIPprint(type("True")) says str, not bool. Quotes make text, even when the text spells a boolean.
CRASHESprint(true) # NameError: name 'true' is not defined
WORKSprint(True) # True

02

Asking questions with comparisons

You rarely type True or False yourself. They arrive as answers to questions. A comparison is a question Python answers with a boolean: == asks 'equal?', != asks 'not equal?', > and < compare size, and >= and <= include the equal case. Python works out any maths first, then compares.

INPUTprint(5 > 3) print(5 < 3) print(5 == 5) print(5 != 5) print(5 >= 5) print(5 <= 4)
OUTPUTTrue False True False True False
TIPPython lets you chain comparisons like in maths: 1 < age < 20 asks 'is age between 1 and 20?' in one line.
METAPHORA comparison is a bouncer at a door: it checks one thing ('are you 18 or older?') and gives one of two answers, yes or no. Where it stops working: a bouncer also decides what happens next, who goes in and who doesn't. A comparison only gives the answer. Deciding what to do with it is the next lesson.

03

= and == are different

The most common mistake in this lesson. One = puts a value into a box; it is an instruction and shows nothing. Two == asks whether two things are equal; it is a question and the answer is a boolean. They look alike and do completely different jobs.

INPUTage = 18 print(age == 18) print(age == 20)
OUTPUTTrue False
TIPRead = out loud as 'put into'. Read == out loud as 'is it the same as?'. When something behaves strangely, check whether you wrote one = where you needed two.
STORESage = 18 # puts 18 into the box
ASKSage == 18 # asks: is it 18?

BLUEPRINT ยท TAP OR HOVER EACH PIECE

Blueprint 1 of 1: storing an answer. Tap each piece.

04

Keeping an answer in a box

An answer is a value like any other, so it can go into a box. Python works out age >= 18 first, then puts the answer in. Watch the box below: it keeps the answer from the moment you asked. It does not update when age changes later.

INPUTage = 20 is_adult = age >= 18 print(is_adult)
OUTPUTTrue
TIPThe box holds a value, not a rule. To get a new answer, ask the question again. That is the labelled-box idea from Lesson 2.
is_adult(empty box)
INPUT(nothing run yet)

05

Comparing text

== works on text too, character by character, and Python is not polite about case: "Apple" and "apple" are different to it. To compare without caring about capitals, put both sides in the same case first with .lower(), the method from Lesson 4.

INPUTname = "ADA" print(name == "ada") print(name.lower() == "ada")
OUTPUTFalse True
TIPname.lower() makes a new lowercase string for the comparison. name itself is not changed, exactly as in Lesson 4.

06

Different kinds of data

A number and text are never equal: 5 == "5" is False. But a whole number and a decimal with the same value are equal: 5 == 5.0 is True. This is the trap from Lessons 6 and 7: input( ) always gives text, so a typed 18 is "18" until you convert it.

INPUTage = "18" print(age == 18) age = int(age) print(age == 18)
OUTPUTFalse True
TIP< and > cannot order a number against text at all; they crash with TypeError. == does not crash, it just says False. Convert first and both behave.
CRASHESprint(5 > "3") # TypeError
WORKSprint(5 > int("3")) # True

07

Quick quiz: what does this print?

What does print(2 + 2 == 5 - 1) show?

08

Quick quiz: what does this print?

The person types 18. What does this print? age = input("Age: ") print(age == 18)

Report a mistake or suggest a lesson