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