Python Booleans
Understand True and False values, evaluate expressions with bool(), and write clear boolean comparisons.
After completing this chapter, you will be able to:
- Understand Python's two boolean values: True and False.
- Evaluate comparison expressions that produce boolean results.
- Use bool() to check how Python treats values as truthy or falsy.
- Avoid common capitalization mistakes with boolean literals.
What Are Booleans?
In programming, programs constantly make yes-or-no decisions: Is the score high enough to pass? Are two passwords equal? Is a cart empty?
Python represents these yes/no states using the boolean (bool) type, which has only two possible values:
TrueFalse
Notice that both True and False start with an uppercase letter and do not have quotes around them.
Booleans hold True or False directly without quotes.
is_logged_in = True
has_errors = False
print(is_logged_in, type(is_logged_in))
print(has_errors, type(has_errors))Expected Output
True <class 'bool'> False <class 'bool'>
Python requires capitalized True and False. Writing true raises a NameError, while 'True' in quotes creates a string instead of a boolean.
is_ready = true
status = "False"is_ready = True
status = FalseBoolean Comparisons
Whenever you compare two values—such as checking if one number is greater than another—Python evaluates the comparison and gives you back a boolean (True or False).
Each comparison produces True or False.
print(10 > 5)
print(10 == 5)
print(4 <= 4)
print("apple" == "orange")Expected Output
True False True False
Evaluating Values with bool()
You can pass any value into the bool() function to see whether Python treats it as True (truthy) or False (falsy).
Almost every non-empty value is True. Only a small set of "empty" or zero values evaluate to False:
FalseNone0and0.0- Empty strings
"" - Empty collections (
[],{},())
Non-zero numbers and non-empty strings evaluate to True.
print("bool('Python'):", bool("Python"))
print("bool(15):", bool(15))
print("bool(''):", bool(""))
print("bool(0):", bool(0))
print("bool(None):", bool(None))Expected Output
bool('Python'): True
bool(15): True
bool(''): False
bool(0): False
bool(None): FalseQuick Check
What will bool('') print in Python?
Chapter Summary
- Booleans represent truth values:
TrueandFalse(always capitalized, no quotes). - Comparisons such as
10 > 5orx == yreturn a boolean value. bool()converts values toTrueunless they are zero,None,False, or empty.