Python bool() function
The Python bool() function returns a Boolean value (either True or False) based on the result of the truth value testing procedure of the given object. If no argument is provided, the function returns False.
The truth value testing for an object can be implemented using if or while conditions or a boolean operation. In the boolean operation, __bool__() and __len__() methods are used. If an object does not have a __bool__() method, then its __len__() method is called, which returns the length of the object. If the length is zero, then the object is considered False, otherwise, it is considered True.
The bool() is one of the built-in functions and you do not need to import any module to use it.
Syntax
Following is the syntax of the Python bool() function −
bool(object)
Parameters
The Python bool() function accepts a single parameter −
object − This parameter indicates an object such as a list, string, number or any expression.
Return Value
The Python bool() function returns either True or False depending on the given input.
bool() function Examples
Practice the following examples to understand the use of bool() function in Python:
Example: Use of bool() Function
The following example shows the basic usage of Python bool() function. If a number is not zero, it is considered true. Therefore, the code below will return false for zero and true for other numbers.
output1 = bool(111)
print("The result of bool operation on positive num:", output1)
output2 = bool(-68)
print("The result of bool operation on negative num:", output2)
output3 = bool(0)
print("The result of bool operation on 0:", output3)
When we run above program, it produces following result −
The result of bool operation on positive num: True The result of bool operation on negative num: True The result of bool operation on 0: False
Example: Check Empty String Using bool()
In the code below, we have two strings and the bool() function is used to check whether the given strings are empty or non-empty. Since any non-empty string is considered True, this function will return False for an empty string and True for a non-empty string.
output1 = bool("Tutorialspoint")
print("The result of bool operation on non-empty String:", output1)
output2 = bool("")
print("The result of bool operation on an empty String:", output2)
Following is an output of the above code −
The result of bool operation on non-empty String: True The result of bool operation on an empty String: False
Example: Check Empty List Using bool() Function
The code below shows how to use bool() function to check truth value of an empty list and a non-empty list.
output1 = bool([71, 87, 44, 34, 15])
print("The result of bool operation on non-empty List:", output1)
output2 = bool([])
print("The result of bool operation on an empty List:", output2)
Output of the above code is as follows −
The result of bool operation on non-empty List: True The result of bool operation on an empty List: False
Example: Demonstrating bool() Vs. __bool()__ Functions
In the following code, we are going to demonstrate how to use the __bool__() and bool() methods together to check if a person is a senior citizen based on their age.
class SeniorCitizen:
def __init__(self, name, age):
self.name = name
self.age = age
def __bool__(self):
return self.age > 60
senCitzn1 = SeniorCitizen("Raman", 20)
senCitzn2 = SeniorCitizen("Ramesh", 65)
output1 = bool(senCitzn1)
output2 = bool(senCitzn2)
print("Is first person is senior citizen:", output1)
print("Is second person is senior citizen:", output2)
Following is an output of the above code −
Is first person is senior citizen: False Is second person is senior citizen: True