Booleans
The bool() Method
- Beginner
- January 7, 2026
Explore how to use Python’s bool() method to convert various data types into boolean values. Understand the conditions that lead to True or False evaluations and learn how custom classes can override this behavior for specific needs.
The bool() method
Python offers a method named bool(). This method evaluates the value passed to it as a parameter as either True or False.
Syntax
bool(add_parameter_here)
Simply put, it converts any object to one of the two boolean values.
Conditions for evaluating values
The bool() method will evaluate all values as True as long as they do not fit the following criteria:
1. False or None values are given
The code below will evaluate to False, as it contains False and None.
print(bool(False))
print(bool(None))
The bool() method evaluating to False in Python
The code below will evaluate to True.
print(bool(True))
print(bool("Hello"))
The bool() method evaluating to True in Python
2. A 0 value of any numeric type (int, float, decimal, etc.)
The code below will evaluate to False, as it contains zero values.
print(bool(0))
print(bool(0.0))
The bool() method evaluating zero values to False in Python
The code below will evaluate to True.
print(bool(1))
print(bool(3.14))
The bool() method evaluating non-zero values to True in Python
3. An empty sequence or collection is given ((), {}, or [])
The code below will evaluate to False due to the empty collections.
print(bool(()))
print(bool({}))
The bool() method evaluating empty collections to False in Python
The code below will evaluate to True as the collections are not empty.
print(bool([1, 2, 3]))
print(bool((0, 1, 3)))
The bool() method evaluating non-empty collections to True in Python
4. If an empty string '' is given
The code below will evaluate to False.
print(bool(''))
The bool() method evaluating empty strings to False in Python
The code below will evaluate to True.
print(bool('Non-empty string'))
The bool() method evaluating non-empty strings to True in Python
5. Overriding the bool() method
Objects or classes that override the bool() method can explicitly define whether they want to be evaluated as true or false.
class MyObject:
def __bool__(self):
return True
Implementing the bool() method in a class in Python
Because our custom class has its own bool() implementation, which is made to return True, the code below will return True.
class MyObject:
def __bool__(self):
return True
custom_instance = MyObject()
print(bool(custom_instance))
Implementing the bool() method in a class in Python
We can also change the implementation to return False.
class MyObject:
def __bool__(self):
return False
custom_instance = MyObject()
print(bool(custom_instance))
Implementing the bool() method in a class in Python
In This Module