ZetCode

Python bool function

last modified November 29, 2026 Python 3.15

In this article we show how to use the bool builtin function, which converts any object to True or False according to its truth value.

bool is also the type of the two constants. Called without an argument it returns False; called with an argument it applies the same truth testing that if and while use.

Truth testing in conditions is introduced in Python data types, the None value has its own article in Python None keyword, and the method behind custom objects is described in Python __bool__ method.

Converting common values

The function is a conversion, not a comparison: it asks the object whether it is true.

basic_bool.py
print(bool())
print(bool(0), bool(1), bool(-1))
print(bool(0.0), bool(0.1))
print(bool(''), bool('0'), bool('False'))
print(bool([]), bool([0]))
print(bool(None), bool(False), bool(True))

Zero, an empty string, an empty list, and None are false; the other values in the example are true.

Note that '0' and 'False' are non-empty strings, so they are true. The call without an argument returns False, like the other empty values.

$ python basic_bool.py
False
False True True
False True
False True True
False True
False False True

The falsy values

Every built-in type has a rule, and the rules can be collected in one list.

falsy.py
falsy = [None, False, 0, 0.0, 0j, '', (), [], {}, set(), range(0)]

for value in falsy:
    print(f'{value!r:12} {bool(value)}')

print(all(not value for value in falsy))

These are the false values of the built-in types: None, False, zero of every numeric type, and empty containers. A range is false when it is empty.

An instance of a class without __bool__ or __len__ is always true, even when it holds no data of its own.

$ python falsy.py
None         False
False        False
0            False
0.0          False
0j           False
''           False
()           False
[]           False
{}           False
set()        False
range(0, 0)  False
True

bool is a kind of integer

bool is a subclass of int with only two instances, so the constants take part in arithmetic.

bool_int.py
print(True + True)
print(True * 10)
print(sum([True, False, True, True]))
print([True, 1] == [1, 1])
print(isinstance(True, int))

flag = True
one = 1
print(flag == one, flag is one)

answers = {True: 'yes', False: 'no'}
print(answers[1])

True behaves as one and False as zero. Summing booleans counts the true ones, a common way to count matches.

Equality and identity differ: flag == one is true, but flag is one is false, because one is an int object and not the True singleton.

The dictionary cannot tell True from 1 either, because they hash and compare equal, so answers[1] finds the entry stored under True.

$ python bool_int.py
2
10
3
True
True
True False
yes

Objects with __bool__ or __len__

A class controls its truth value with __bool__. Without that method, Python falls back to __len__.

custom_truth.py
class Basket:
    def __init__(self, items):
        self.items = items

    def __len__(self):
        return len(self.items)

    def __repr__(self):
        return f'Basket({self.items!r})'


class Ticket:
    pass


print(bool(Basket([])), bool(Basket(['apple'])))
print(bool(Ticket()))

The basket is false when it is empty because its length is zero. A class with neither method is always true, which is why the ticket prints True.

How to define __bool__, and why it must return a real boolean, is described in Python __bool__ method.

$ python custom_truth.py
False True
True

bool does not parse text

This is the most common surprise: the function tests emptiness, not meaning.

parse.py
def parse_flag(text):
    return text.strip().lower() in {'1', 'true', 'yes', 'on'}


for raw in ('true', 'False', '0', ' YES ', 'off'):
    print(f'{raw!r:8} {str(bool(raw)):5} {parse_flag(raw)}')

print(bool(int('0')), bool(int('1')))

Every non-empty string is true, including 'False', '0', and 'off'. Configuration values have to be compared with a set of accepted spellings.

For whole-number text, bool(int(text)) is enough, because the conversion rejects anything that is not a number.

$ python parse.py
'true'   True  True
'False'  True  False
'0'      True  False
' YES '  True  True
'off'    True  False
False True

Returning a real boolean

The and operator returns one of its operands, not a boolean, which leaks through a function that claims to return bool.

normalize.py
def first_active(users):
    return users and users[0]


users = [{'name': 'John', 'active': True}]

print(first_active(users))
print(first_active([]))
print(isinstance(first_active(users), bool))
print(bool(first_active(users)), bool(first_active([])))

The function returns a dictionary or an empty list, so the isinstance check is false. Wrapping the result in bool gives the promised type, which matters when the value is stored in a database column, written to JSON, or compared with is True.

The same rule explains why [] or None is None rather than False.

$ python normalize.py
{'name': 'John', 'active': True}
[]
False
True False

Special numeric values

Exact and floating-point types decide truthiness by their own rules, so the usual table does not cover everything.

special_numbers.py
from decimal import Decimal
from fractions import Fraction

print(bool(float('nan')))
print(bool(Decimal('0')), bool(Decimal('-0.00')), bool(Decimal('0.01')))
print(bool(Decimal('NaN')))
print(bool(Fraction(0, 5)), bool(Fraction(-1, 5)))

A NaN float is not equal to itself, but it is not zero, so it is true. Decimal and Fraction recognise exact zero, including a negative zero with no digits.

Truthiness belongs to the type: bool asks the object, it does not compare the object with zero.

$ python special_numbers.py
True
False False True
True
False True

Source

Python bool() documentation

Python truth value testing

Python object.__bool__()

In this article we have used bool to convert values, to explain which values are false, and to normalise results that must be real booleans. Combining truth tests is shown in Python any and all builtins, and the boolean operators in Python and keyword.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Python tutorials.