Python bool() Builtin Function
Python bool() builtin function takes an object as argument and returns True if the object evaluates to true, or False otherwise.
In this tutorial, we will learn how to create a boolean value using bool() builtin function.
Syntax
The syntax to create a boolean value from Python object is
result = bool(obj)
where obj
is any Python object and result
is a boolean value.
bool() function by default, with no argument passed, returns False.
Example
In the following program, we will take take an integer value in variable obj
and find its boolean value using bool() builtin function.
Python Program
obj = 87
result = bool(obj)
print(f'bool({obj}) returns {result}')
Output
bool(87) returns True
In the following program, we will take take an integer value of zero in variable obj
and find its boolean value using bool() builtin function.
Python Program
obj = 0
result = bool(obj)
print(f'bool({obj}) returns {result}')
Output
bool(0) returns False
In the following program, we will take a non-empty list and find its boolean value.
Python Program
obj = [14, 52, 3]
result = bool(obj)
print(f'bool({obj}) returns {result}')
Output
bool([14, 52, 3]) returns True
In the following program, we will take an empty list and find its boolean value.
Python Program
obj = []
result = bool(obj)
print(f'bool({obj}) returns {result}')
Output
bool([]) returns False
Conclusion
In this Python Tutorial, we learned about bool() builtin function in Python, with the help of example programs.