True, False, and Comparisons
A boolean is one of exactly two values: True or False. They are the answers your program gives to yes-or-no questions, and almost every decision comes down to one.
Comparisons make booleans
Comparing two values produces a boolean. Notice that checking equality uses two equals signs, ==, because a single = already means "assign":
print(5 == 5) # True (are they equal?)
print(5 != 3) # True (are they different?)
print(5 > 3) # True
print(5 < 3) # False
print(5 >= 5) # True (greater than OR equal)
print(5 <= 4) # FalseYou can store the result in a variable like any other value:
age = 20
is_adult = age >= 18
print(is_adult) # TrueTruthiness
Python also treats some everyday values as "truthy" or "falsy" when it needs a yes or no. The falsy ones are worth memorising: 0 (and 0.0), the empty containers ("", [], {}, (), and an empty set), None, and False itself. Almost everything else is truthy.
print(bool(0)) # False
print(bool("")) # False
print(bool("hi")) # True
print(bool(42)) # TrueSo an empty box counts as "no" and a filled one counts as "yes". That single idea powers a huge amount of real code.
Set age = 20. Make a boolean is_adult that is True when age is 18 or more (use >=). Make another boolean is_empty that is True when the string name = "" is empty (hint: not name, since an empty string is falsy). Print both, each on its own line.
This lesson is locked
Lessons open one at a time. Finish the previous lesson to unlock this one.