A boolean is a value that can only be True or False. It’s named after the mathematician George Boole, who invented the logic these values follow.

Creating booleans

is_open: bool = True
has_account: bool = False

Two things to notice:

  • The values are spelled True and False — capital T and capital F. true/false are not valid Python.
  • The type is bool, short for “boolean”.

Booleans come from comparisons

You’ll rarely write True or False directly. Most booleans appear as the result of a comparison:

age: int = 18
is_adult: bool = age >= 18
print(is_adult)   # True

score: int = 65
passed: bool = score > 70
print(passed)     # False

We’ll cover comparison operators in detail in the next section.

Combining booleans

You can combine booleans using and, or, and not:

age: int = 20
has_id: bool = True

can_enter: bool = age >= 18 and has_id
print(can_enter)   # True
  • and — both sides must be True
  • or — at least one side must be True
  • not — flips the value (not True is False)

Truthy and falsy values

In Python, every value behaves like a boolean when used in an if condition. Some values are falsy (treated as False); everything else is truthy (treated as True).

The falsy values are:

  • False
  • None
  • The number 0 (and 0.0)
  • Empty containers: "", [], {}, (), set()

Everything else is truthy, including any non-zero number and any non-empty string.

if "Hello":
    print("string is truthy")     # this runs

if "":
    print("empty string is true")  # this does not run

if 0:
    print("zero is truthy")        # this does not run

if [1, 2, 3]:
    print("list has items")        # this runs

This is one of Python’s most useful shortcuts — you’ll often write if my_list: to mean “if the list isn’t empty”.

Booleans are numbers in disguise

Here’s a Python quirk worth knowing: True equals 1 and False equals 0. You can do maths with them.

print(True + True)         # 2
print(True * 10)           # 10
print(False + 5)           # 5

You’ll rarely use this directly, but it’s the reason sum([True, False, True]) returns 2 — a quick way to count how many items in a list match a condition.

Comparing booleans

To check if something is a boolean, use is, not ==:

flag = True

print(flag is True)    # True
print(flag == True)    # True (works but considered less idiomatic)

To check if a value looks true (truthy), just use the value itself in an if:

# Don't write:
if flag == True:
    ...

# Write:
if flag:
    ...

The shorter version reads more naturally and is the standard Python style.

What’s next

You can store true/false answers. Next, we’ll meet strings — Python’s type for text.

Toggle theme (T)