W3docs

Understanding Python Booleans

Learn Python booleans: True/False values, truthy and falsy rules, bool() conversion, logical operators, and common pitfalls with clear examples.

A boolean is a data type with exactly two possible values: True or False. Python booleans control every decision your code makes — from if statements to while loops to list comprehensions. This chapter covers how booleans work, how Python evaluates any value as truthy or falsy, and practical patterns you will use every day.

What Are Python Booleans?

In Python, True and False are keywords (capitalized). They belong to the built-in type bool, which is a subclass of int. This means True behaves like 1 and False behaves like 0 in arithmetic contexts.

print(type(True))   # <class 'bool'>
print(type(False))  # <class 'bool'>

print(True == 1)    # True
print(False == 0)   # True
print(True + True)  # 2  (bool is a subclass of int)

The last line — True + True equaling 2 — surprises many beginners. It is valid Python and occasionally useful (for example, counting how many conditions in a list are true), but using booleans in arithmetic should be intentional and clearly commented.

Creating Boolean Variables

You can assign True or False directly to a variable:

Python define boolean variable

python— editable, runs on the server

By convention, boolean variable names often start with is_, has_, can_, or should_ to make their purpose clear at a glance.

Booleans from Comparison Operators

Every comparison operator produces a boolean result:

OperatorMeaningExampleResult
==equal to5 == 5True
!=not equal to5 != 3True
<less than3 < 5True
>greater than5 > 3True
<=less than or equal5 <= 5True
>=greater than or equal4 >= 5False

Python compare two integers

python— editable, runs on the server

You can store comparison results in a variable and reuse them, which keeps complex if conditions readable.

Using Booleans in Conditional Statements

Booleans are the engine behind if/else decisions. Python evaluates the expression after if and executes the indented block only when the result is True:

Python compare two integers and print the result

python— editable, runs on the server

You can also use a boolean variable directly without any comparison operator:

is_raining = True
if is_raining:
    print("Bring an umbrella")
else:
    print("Enjoy the sunshine")

Using if is_raining: is more Pythonic than if is_raining == True:. The latter adds no information and is considered a code smell.

Truthy and Falsy Values

Python does not require an actual True or False value in a boolean context. Every object is either truthy (treated as True) or falsy (treated as False). This lets you write concise conditions without explicit comparisons.

Falsy values

The following values are always falsy in Python:

ValueType
Falsebool
0int
0.0float
"" or ''str (empty string)
[]list (empty list)
{}dict (empty dict)
()tuple (empty tuple)
set()set (empty set)
NoneNoneType

Everything else is truthy — including non-zero numbers, non-empty strings, and non-empty collections.

# All of these print "empty" because the values are falsy
for value in [0, 0.0, "", [], {}, (), None]:
    if not value:
        print(f"{repr(value)} is falsy")

Truthy values in practice

name = input("Enter your name: ")
if name:                   # truthy if name is not an empty string
    print(f"Hello, {name}!")
else:
    print("No name provided.")

items = [1, 2, 3]
if items:                  # truthy if list is not empty
    print(f"Processing {len(items)} items")

This pattern — checking a collection directly rather than len(items) > 0 — is idiomatic Python.

Converting Values to Boolean with bool()

The built-in bool() function converts any value to its boolean equivalent, which is useful when you want to inspect whether something is truthy or falsy:

Python casting into boolean

python— editable, runs on the server

More examples:

print(bool(0))        # False
print(bool(0.0))      # False
print(bool(""))       # False
print(bool([]))       # False
print(bool(None))     # False

print(bool(1))        # True
print(bool(-1))       # True
print(bool("hello"))  # True
print(bool([0]))      # True  — list with one element is truthy

Note that bool([0]) is True even though the single element inside is falsy. Python checks whether the container is empty, not the values inside it.

Logical Operators with Booleans

Python provides three logical operators for combining boolean expressions. See Python Operators for the full operator reference.

and

Returns True only if both operands are true:

print(True and True)   # True
print(True and False)  # False
print(False and True)  # False
print(False and False) # False

or

Returns True if at least one operand is true:

print(True or False)   # True
print(False or False)  # False
print(True or True)    # True

not

Inverts the boolean value:

print(not True)   # False
print(not False)  # True
print(not 0)      # True  (0 is falsy, so not 0 is True)
print(not "hi")   # False ("hi" is truthy, so not "hi" is False)

Short-circuit evaluation

Python evaluates and and or lazily — it stops as soon as the result is determined. This is called short-circuit evaluation:

  • False and <anything> — Python never evaluates the right side because the result is already False.
  • True or <anything> — Python never evaluates the right side because the result is already True.

This matters when the right side has side effects or could raise an error:

items = []

# Safe: the second condition is only evaluated if items is truthy
if items and items[0] > 10:
    print("First item exceeds 10")

Without short-circuiting, items[0] on an empty list would raise an IndexError. Because items is falsy (empty list), Python skips the right side entirely.

The is Operator vs. == with Booleans

== tests whether two values are equal. is tests whether two names refer to the same object in memory.

print(1 == True)   # True  (equal in value)
print(1 is True)   # False (different objects)

Always use == (or rely on truthiness) when comparing values. Reserve is for identity checks — most commonly is None:

value = None
if value is None:
    print("No value provided")

Using == None works but is None is the idiomatic and slightly faster form.

Counting with Booleans

Because True == 1 and False == 0, you can use sum() to count how many items in a list satisfy a condition:

scores = [85, 42, 91, 67, 55, 78]
passed = sum(score >= 60 for score in scores)
print(f"{passed} out of {len(scores)} students passed")
# 4 out of 6 students passed

This is more concise than a manual counter loop and is a common Python idiom.

Common Pitfalls

1. Comparing with True/False explicitly

# Avoid
if is_valid == True:
    ...

# Prefer
if is_valid:
    ...

2. Confusing = (assignment) with == (equality)

x = 5
if x = 5:   # SyntaxError — use == for comparison
    print("equal")

3. Assuming an empty list inside a list is falsy

outer = [[]]       # a list containing one empty list
if outer:
    print("truthy")  # This prints! outer has one element.

The outer list has one item (the inner empty list), so outer itself is truthy. Only the inner list outer[0] is falsy.

Summary

  • Python booleans are True and False — always capitalized.
  • Every value in Python is either truthy or falsy. Falsy values include 0, "", [], {}, (), and None.
  • Use bool() to explicitly convert a value to its boolean equivalent.
  • Combine conditions with and, or, and not. Python evaluates them lazily via short-circuit rules.
  • Use is None (not == None) when checking for None.
  • Because bool is a subclass of int, True + True equals 2 — handy for counting.

Next, learn about Python Operators to see how comparison and logical operators produce the boolean values covered here. You can also explore how booleans drive program flow in Python If...Else.

Practice

Practice
Which of the following values are falsy in Python?
Which of the following values are falsy in Python?
Was this page helpful?