booleans

To make decisions, we need a concept of true/false. That’s so we can say “if the user entered a number greater than 5 is true, tell them good job. If it is false, call them a moron.” In programming, true/false values are called “Boolean” values, or booleans. They’re named after George Boole, a guy who did a lot of fancy stuff with true/false values.

You already know that variables can hold numbers and strings, but they can also hold booleans:

foo = True
print(foo)
bar = False
print(bar)
True
False

Notice that we typed foo = True, not foo = "True". Usually if you leave the quotes off, you’ll get an error:

foo = Asparagus
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'Asparagus' is not defined

But True and False are special keywords, so we don’t (and shouldn’t) use quotes around them.


previous: exercises next: boolean expressions