In this article we will discuss about the bool() function in python programming language with the help of examples.
Python bool() function
The built-in python function uses the standard testing procedure to convert and return a value into Boolean value (True and False). If the is true, it returns and If the is false or omitted, it returns .
Syntax
bool([value])
Parameter of bool() function in python
The python function takes only one parameter i.e. value. It is optional to pass the value to a bool() function but it returns False if the you don't have passed the value to the bool() function.
Return value of Python bool() function
The bool() function in python returns following values;
• If the is true, it returns .
• If the is false or omitted, it returns .
• None, empty sequence, zeros of any numeric type and empty mapping in python are considered as false.
Python bool() Example
In the following example we have converted values into boolean values.
value = []print("Boolean of [] is: ", bool(value))
value = [0]
print("Boolean of [0] is: ", bool(value))
value = 0
print("Boolean of 0 is: ", bool(value))
value = 1
print("Boolean of 1 is: ", bool(value))
value = -1
print("Boolean of -1 is: ", bool(value))
value = None
print("Boolean of None is: ",bool(value))
value = True
print("Boolean of True is: ", bool(value))
value = False
print("Boolean of False is: ", bool(value))
value = 'a string'
print("Boolean of a string is: ", bool(value))
Output
Boolean of [] is: FalseBoolean of [0] is: True
Boolean of 0 is: False
Boolean of 1 is: True
Boolean of -1 is: True
Boolean of None is: False
Boolean of True is: True
Boolean of False is: False
Boolean of a string is: True