Python Booleans

A Boolean is a value that is either True or False, and it drives every decision your program makes.

The Two Boolean Values

Python has exactly two Boolean values, written as True and False with a capital first letter. They belong to the bool type and are the answers to yes-or-no questions inside your code, such as whether one number is larger than another.

Comparisons produce Booleans

print(10 > 9)
print(10 == 9)
print(10 < 9)

result = 5 + 5 == 10
print(result)
Note: True and False must be capitalized. Writing true or false in lowercase produces a NameError because Python does not recognize them as the built-in Boolean values.

Truthiness of Other Values

Almost any value can be tested for truth, not just True and False. Pass a value to bool() to see how Python judges it. Empty containers, the number zero, and None are treated as False, while most other values are treated as True.

  • Falsy values include False, 0, 0.0, empty strings, empty lists, empty dictionaries, and None.
  • Truthy values include any non-zero number and any non-empty string or collection.
  • This lets you write plain checks like `if name:` to mean 'if name is not empty'.

Testing truthiness

print(bool(0))
print(bool(""))
print(bool([]))
print(bool(42))
print(bool("hi"))

Booleans in Decisions

Booleans come to life inside if statements and while loops, where they decide which branch of code runs. You can combine them with the logical operators and, or, and not to express more detailed conditions.

Driving an if statement

age = 20
has_ticket = True

if age >= 18 and has_ticket:
    print("Entry allowed")
else:
    print("Entry denied")

print(not has_ticket)
ExpressionResultReason
bool(0)FalseZero is falsy
bool("hi")TrueNon-empty string is truthy
True and FalseFalseBoth must be True for and
True or FalseTrueor needs only one True
not TrueFalsenot flips the value

Exercise: Python Booleans

What are the only two literal values of Python's bool type?