Different values in Python can be described as being "truthy" or "falsy" even if they aren't Boolean
values, which means they are interpreted as True
or False
in a situation that expects a Boolean
value (such as an if
condition). As defined in the documentation, every value in Python, regardless of type, is interpreted as being True
except for the following values (which are interpreted as False
):
- Constants defined to be false:
None
and False
.
- Zero of any numeric type:
0
, 0.0
, 0j
, Decimal(0)
, Fraction(0, 1)
- Empty sequences and collections:
''
, ()
, []
, {}
, set()
, range(0)
To your specific situation, using the if
situation, the following statement:
if None:
# some code here
would be functionally identical to:
if False:
# some code here
This is because, as shown in the list above, the value None
is automatically converted to False
for the purposes of the if
condition. This is something referred to as "syntactic sugar", which is a feature of the language that exists to make the developer's life easier.
However, just because None
is interpreted as False
in this particular scenario, that doesn't mean the two values are equal to each other. This is because False
is meant to be part of the True/False pair indicating binary concepts like "yes/no", "on/off", etc. None
, on the other hand, represents the concept of nothing. Variables with a value of None
means they have no value at all. To compare it to False
in the form of a metaphor, False
would be like answering somebody by saying "No", where None
would be like not answering them at all.
As a more practical example, see the following code snippet:
if None == False:
# code in here would not execute because None is not equal to False