In Python, any non-zero value, non-empty string/list/set, etc is considered as True
where as None
, empty-string/list/set etc and 0
are considered as False
by if
. This evalution is done by the bool()
. Below is the illustration.
my_list = ['a', 100, None, '', 0, {}, {'a': 1}, [], ['a']]
for item in my_list: # Iterate over each value and checks the condition
if item:
print item, ": True", ", Bool value: ", bool(item)
else:
print item, ": False", ", Bool value: ", bool(item)
which prints (explicitly formatted for readability):
a : True , Bool value: True
100 : True , Bool value: True
None : False , Bool value: False
: False , Bool value: False
0 : False , Bool value: False
{} : False , Bool value: False
{'a': 1} : True , Bool value: True
[] : False , Bool value: False
['a'] : True , Bool value: True
As you see, execution of if
is mapped to the bool()
value.