It’s not mandatory to pass the value to bool(). If you do not pass a value, bool() returns False. Python bool() function returns the boolean value of a specified object.
Python bool()
Python bool() is a built-in function that converts the value to Boolean (True or False) using the standard truth testing procedure.
The boolean builtins are capitalized: True and False.
The object will always return True, unless:
The object is empty, like [], (), {}
The object is False
The object is 0
The object is None
See the following syntax.
bool(object)
The object parameter is like a String, List, Number, etc.
See the following example.
app = False print(bool(app)) app = True print(bool(app)) app = 5 data = 10 print(bool(app==data)) app = None print(bool(app)) app = () print(bool(app)) app = {} print(bool(app)) app = 0.0 print(bool(app)) app = 'MillieBobbyBrown' print(bool(app))
See the following output.
➜ pyt python3 app.py False True False False False False False True ➜ pyt
The bool() returns the following output:
- False if a value is omitted or false
- True if a value is true
See the other examples.
def check(num): return(bool(num % 2 == 0)) num = 11 if(check(num)): print("Even") else: print("Odd")
See the output.
➜ pyt python3 app.py Odd ➜ pyt
Python bool() Standard Rules
Python bool() function uses the standard truth testing rules to convert a specified parameter object to the Boolean value.
The main rules used to return the Boolean value are the following.
- Any object Boolean value is considered True if it is not implementing the __bool__() function and __len__() functions.
- If the object doesn’t define the __bool__() function but defines the __len__() function, then the __len__() function is used to get a boolean value of the object. If the __len__() returns 0, then bool() function will return False otherwise True.
- The boolean value will be False for the None and False constants.
- The boolean value will be False for zero value such as the 0, 0.0, 0j, Decimal(0), and Fraction(0, 1).
- The boolean value will be False for empty data structures like tuple, dict, and collections, such as ”, (), [], {} etc.
Python bool() function with custom object
Let’s see what happens with the custom object. I will not define __bool__() and __len__() functions for the object. See the following code example.
# app.py class App: number = 0 def __init__(self, i): self.number = i a = App(0) print(bool(a)) d = App(10) print(bool(d))
See the following output.
➜ pyt python3 app.py True True ➜ pyt
Since in the above example, none of __bool__() and __len__() functions are defined, object boolean value is returned as True. Let’s add __len__() function to the App class.
class App: number = 0 def __init__(self, i): self.number = i def __len__(self): print('len function called') if self.number > 0: return self.number else: return 0 a = App(0) print(bool(a)) d = App(11) print(bool(d))
See the following output.
➜ pyt python3 app.py len function called False len function called True ➜ pyt
It’s clear that the __len__() function is called by bool(). When 0 is returned, the bool() function returns False.
The bool() function returns True when a positive integer is returned.
Now let’s add the __bool__() function to the App class.
class App: number = 0 def __init__(self, i): self.number = i def __len__(self): print('len function called') if self.number > 0: return self.number else: return 0 def __bool__(self): print('bool function called') return self.number > 0 a = App(0) print(bool(a)) d = App(11) print(bool(d))
See the following output.
➜ pyt python3 app.py bool function called False bool function called True ➜ pyt
So, now it is clear that if both __bool__() and __len__() functions are defined for the object, then the __bool__() function is used to get the Boolean value of an object.
Python bool() with numbers
Let’s test the bool() method with numbers.
print(bool(11)) print(bool(11.19)) print(bool(0xF)) print(bool(11 - 10j))
See the following output.
➜ pyt python3 app.py True True True True ➜ pyt
Python bool() with Strings
Let’s test the book() method with Strings.
print(bool('Krunal')) print(bool('')) print(bool("AppDividend"))
See the following output.
➜ pyt python3 app.py True False True ➜ pyt
That’s it.