Python Booleans and Comparison Operators
True and False
A boolean (bool) has only two values: True and False. You met them in chapter 4. In this topic, you'll see where they really come from: questions your program asks.
Think of a boolean as a light switch: on or off, yes or no. Nothing in between.
is_raining = False
has_umbrella = True
print(is_raining, has_umbrella)
print(type(is_raining))False True <class 'bool'>
The rules, again
- Capital first letter:
True,False. - No quotes:
"True"is a string, not a boolean.
print(type(True))
print(type("True"))<class 'bool'> <class 'str'>
Booleans are answers
You'll rarely type True or False yourself. Most booleans come from comparing values: "is the score above 50?", "is the password right?". Python answers each question with True or False.
score = 72
print(score > 50)True
That > is a comparison operator. You'll learn all of them in the next lesson. In chapter 8, if will use these answers to make decisions, like the diamond in a flowchart.
A fun fact
Behind the scenes, True counts as 1 and False as 0:
print(True + True)2
You won't need this often, but it explains some surprising results later.
Booleans are True or False: the answers to yes-or-no questions.