This website collects cookies to deliver better user experience
Python bool()
Python bool()
ItsMyCode |
Python’s bool() function converts a given value into Boolean(True or False) using the standard truth testing procedure.
bool() Syntax
The syntax of bool() method is
**bool([value])**
bool() Parameters
The bool() method can take one argument on which the standard truth testing procedure is applied.
The parameter is optional for the bool() function. If you do not pass any value by default, it returns False.
bool() Return Value
The bool() function returns
True if the parameter of the value passed is True
False if the parameter of the value passed is False or if the value is omitted
There are few exceptional cases where the bool() method returns False. Following are the values:
None
False
Empty sequence such as (), ‘ ‘ ,[] etc
Zero of any numeric type such as 0 , 0.0 , 0j
Empty mapping such as {}
If Objects of Classes having __bool__ () or __len()__ method, returning 0 or False
Example of bool() function in Python
# Python program to demostrate bool() function# Returns False as x is Falsex =Falseprint(x,'is ',bool(x))# Returns True as x is Truex =Trueprint(x,'is ',bool(x))# Returns False as x is an empty sequencex =()print(x,'is ',bool(x))# Returns False as x is an empty mappingx ={}print(x,'is ',bool(x))# Returns False as x is 0x =0.0print(x,'is ',bool(x))# Returns False as x is Nonex =Noneprint(x,'is ',bool(x))# Returns True as x is a non empty stringx ='ItsMyCode'print(x,'is ',bool(x))
python
Output
False is False
True is True
() is False
{} is False
0.0 is False
None is False
ItsMyCode is True