Course outline · 0% complete

0/27 lessons0%

Course overview →

True or False: booleans and comparisons

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

Quiz

Warm-up from lesson 3-2: after count = 4 and then count = count + 1, what is stored in count?

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/no question — that is what this lesson adds.

Strings hold text, 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 no quotes, 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: = stores a value into a variable (lesson 3-1), 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 =, "equals" for ==.

Code exercise · python

Predict each line's output (True or False), then run to check yourself.

Quiz

What is the difference between = and == in Python?

Code exercise · python

Your turn. Write two print lines that output True then False, using comparisons (not by printing the words directly): first compare 100 >= 100, then compare the strings "cat" and "dog" for equality.