Course outline · 0% complete

0/27 lessons0%

Course overview →

True or False: booleans and comparisons

lesson 4-1 · ~9 min · 12/27

Lesson 3-2 covered what is stored in count after count = 4 and then count = count + 1.

5. The right side runs first with the current value, giving 4 + 1 = 5, and then 5 replaces the old contents of the box.

You will use this exact update pattern constantly once loops arrive in unit 5, where a variable is updated once per repetition.

A third type: True or False

Every interesting feature of software is a decision: is this password correct, is the cart total over the free-shipping threshold, is this email spam? Before a program can decide anything, it needs a type whose whole job is to hold the answer to a yes-or-no question, and that is what this lesson adds.

Strings hold text, and ints and floats hold numbers. The third essential type is the boolean, named after the logician George Boole. A boolean has exactly two possible values, True or False. Note the capital first letter and the absence of quotes, because these are not strings.

Booleans usually come from comparisons:

OperatorQuestion it asksExampleResult
==equal?5 == 5True
!=not equal?7 != 7False
<less than?10 < 2False
>greater than?5 > 3True
<=less than or equal?3 <= 3True
>=greater or equal?2 >= 9False

The big trap is that = stores a value into a variable, as lesson 3-1 covered, while == asks whether two values are equal. Mixing them up is one of the most common beginner errors, so read them aloud differently: "gets" for =, and "equals" for ==.

Three comparisons, three booleans

Each line asks a question and prints the answer.

print(5 > 3)
print(5 == 5)
print(7 != 7)

Output

True
True
False

5 > 3 asks whether 5 is greater than 3, and 7 != 7 asks whether 7 is different from 7, which it is not.

Worth noticing: print is doing nothing special here. Each comparison is evaluated down to a single boolean value before print ever sees it, exactly like the arithmetic in lesson 2-2. A comparison is an expression that produces a value, and True is as ordinary a value as 42.

The difference between = and ==

= stores a value in a variable, and == asks whether two values are equal.

= is assignment from lesson 3-1, meaning put this value in the box. == is comparison, asking whether these two values are the same, and it produces a boolean.

A single = where you meant == is a classic bug. In Python it is usually a friendly one, because if age = 18: is a syntax error that refuses to run at all.

That safety net is not universal. Several languages accept an assignment where a condition belongs, quietly store the value, and treat the result as the condition, which produces a program that always takes the same branch for no visible reason.

Comparing numbers and strings

Two comparisons, producing True then False.

print(100 >= 100)
print("cat" == "dog")

Output

True
False

Reading the pieces

  • 100 >= 100 is True because >= accepts equality. Only > would reject it, and confusing the two is how off-by-one bugs are born.
  • Comparisons work on strings too. "cat" == "dog" asks whether the two strings are identical character for character.
  • String equality is exact, which includes capitalization. "Cat" == "cat" is False, and that surprises people the first time a login check rejects a correct username.