Python bool() Function
The Python bool() function is used to return boolean value of an object. In Python, everything is an object and when an object is passed as parameter in bool() function, it always returns True unless:
- The object is empty, like [], (), {}.
- The object is False
- The object is 0
- The object is None
Syntax
bool(object)
Parameters
object |
Required. Any Python object. |
Example:
In the below example, the bool() function is used on variables called MyString, MyList and MyNumber to return boolean value of these objects. As these objects are either empty or None, hence returns False.
MyString = "" print(bool(MyString)) MyList = [] print(bool(MyList)) MyNumber = None print(bool(MyNumber))
The output of the above code will be:
False False False
Example:
In the below example, the bool() function is used on variables called MyString, MyList and MyNumber to return True.
MyString = "Hello" print(bool(MyString)) MyList = [1, 2, 3] print(bool(MyList)) MyNumber = 15 print(bool(MyNumber))
The output of the above code will be:
True True True
❮ Python Built-in Functions