The syntax of bool is:
bool([value])
It's not mandatory to pass a value to bool(). If you do not pass a value, bool() returns False
.
In general use, bool() takes a single parameter value
.
The bool() returns:
False
if the value is omitted or falseTrue
if the value is trueThe following values are considered false in Python:
None
False
0
, 0.0
, 0j
()
, []
, ''
.{}
__bool__()
or __len()__
method which returns 0
or False
All other values except these values are considered true.
test = [] print(test,'is',bool(test)) test = [0] print(test,'is',bool(test)) test = 0.0 print(test,'is',bool(test)) test = None print(test,'is',bool(test)) test = True print(test,'is',bool(test)) test = 'Easy string' print(test,'is',bool(test))
When you run the program, the output will be:
[] is False [0] is True 0.0 is False None is False True is True Easy string is True
It takes a lot of effort and cost to maintain Programiz. We would be grateful if you support us by either:
Disabling AdBlock on Programiz. We do not use intrusive ads.
or
Donate on Paypal