Booleans
A boolean represents one of two possible states: True or False. Booleans are the backbone of every decision your program makes — every if statement, while loop condition, and comparison ultimately resolves to one of these two values.
True and False Are Keywords
In Python, True and False are reserved keywords, always written with a capital first letter (unlike true/false in JavaScript or Java). They belong to the built-in bool type.
is_active = True has_permission = False print(type(is_active)) # <class 'bool'> print(is_active) # True # true = True -- this would just create a normal variable named "true"; # it is NOT the same as the keyword True
bool Is a Subclass of int
Booleans are, under the hood, a specialized form of integer. True behaves like 1 and False behaves like 0 in arithmetic and comparisons.
print(True == 1) # True print(False == 0) # True print(True + True) # 2 -- booleans can be added like integers print(isinstance(True, int)) # True -- bool IS an int subclass scores = [90, 85, 100] passing = [s >= 90 for s in scores] print(sum(passing)) # 2 -- True is counted as 1 when summed
Truthy and Falsy Values
Every object in Python has an inherent truthiness — it can be used anywhere a boolean is expected, such as in an if condition. Most objects are truthy; a specific, well-defined set of values is falsy.
Falsy Value | Type |
|---|---|
False | bool |
0, 0.0, 0j | int, float, complex |
'' (empty string) | str |
[] (empty list) | list |
() (empty tuple) | tuple |
{} (empty dict) | dict |
set() (empty set) | set |
None | NoneType |
Everything else — non-zero numbers, non-empty strings and collections, and virtually every other object — is truthy.
values = [0, 1, "", "hi", [], [1, 2], {}, None, 3.14]
for v in values:
print(f"{v!r}: {'truthy' if v else 'falsy'}")
# 0: falsy
# 1: truthy
# '': falsy
# 'hi': truthy
# []: falsy
# [1, 2]: truthy
# {}: falsy
# None: falsy
# 3.14: truthyUsing bool() to Check Truthiness
The built-in bool() function converts any value to its explicit True/False equivalent, which is a quick way to verify how Python will treat something in a conditional context.
print(bool(0)) # False
print(bool(42)) # True
print(bool("")) # False
print(bool("no")) # True -- even the string "False" would be truthy, since it's non-empty!
print(bool([])) # False
print(bool([0])) # True -- a list containing one element (even a falsy one) is truthyComparison Operators Return Booleans
Every comparison operator (==, !=, <, >, <=, >=) evaluates to a boolean, which is why they combine naturally with if statements and logical operators.
age = 20 print(age == 20) # True print(age != 18) # True print(age < 18) # False print(age >= 18) # True can_vote = age >= 18 print(can_vote) # True
Combining Booleans
Python provides and, or, and not for combining boolean expressions. These follow their own short-circuiting rules and are covered fully in the dedicated tutorial on logical operators.
age = 20 has_id = True can_enter = age >= 18 and has_id print(can_enter) # True
Key Takeaways
TrueandFalseare capitalized keywords belonging to thebooltype.boolis a subclass ofint— True behaves as 1, False as 0.A specific set of values are falsy: 0, 0.0, "", [], (), {}, set(), and None. Everything else is truthy.
bool(value)reveals how Python will treat any object in a boolean context.All comparison operators produce boolean results.