Python bool() function



The Python bool() function is a built-in function that returns a Boolean value (either True or False) based on the result of truth value testing procedure of the given object. It is not necessary to pass any arguments to this function and if we don't pass, it will return 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.

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.

Examples

Let's understand the working of bool() function with the help of some examples −

Example 1

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 2

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 3

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 4

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
python_built_in_functions.htm
Advertisements