Last updated: Jun 15, 2022
Photo from Unsplash
is operator to check if a variable is null in Python, e.g.
if my_var is None:. The
is operator returns
True if the values on the
left-hand and right-hand sides point to the same object and should be used when
checking for singletons like
my_var = None # ✅ Check if variable is None if my_var is None: # 👇️ this runs print('variable is null') # ✅ Check if variable is NOT None if my_var is not None: print('variable is not null')
Note that there isn't a
null value in Python.
Noneobject represents the absence of a value. This is the equivalent of
nullin other languages.
If you need to check if a variable exists and has been declared, use a
try: does_this_exist print('variable exists') except NameError: print('variable doesnt exist')
ifstatement checks if the variable stores a
Nonevalue, and the second - if the variable doesn't store a
You should use the
is operator when you need to check if a variable stores a
When we use
is, we check for the object's identity.
pep 8 style guide
mentions that comparison to singletons like
None should always be done with
is not, and never the equality operators.
==and not equals
!=) when you need to check if a value is equal to another value, e.g.
'hello' == 'hello'.
Here is an example that better illustrates checking for identity (
checking for equality (
my_first_list = ['a', 'b', 'c'] my_second_list = my_first_list # 👈️ same list as above print(my_first_list is my_second_list) # 👉️ True print(my_first_list == my_second_list) # 👉️ True
We declared 2 variables that store the same list.
We set the second variable to the first, so both variables point to the same
list object in memory.
Now, let's create a shallow copy of the list and assign it to the second variable.
my_first_list = ['a', 'b', 'c'] my_second_list = my_first_list.copy() # 👈️ copy created print(my_first_list is my_second_list) # 👉️ False print(my_first_list == my_second_list) # 👉️ True
When we use the double equals operator, Python calls the
__eq__() method on
x.__eq__(y). In theory this method could be implemented
in an unpredictable way, so checking for
None with the
is operator is more
You can use the id() function to get the identity of an object.
my_first_list = ['a', 'b', 'c'] print(id(my_first_list)) # 👉️ 139944523741504 my_second_list = my_first_list.copy() print(id(my_second_list)) # 👉️ 139944522293184 print(id(my_first_list) == id(my_second_list)) # 👉️ False
id() function returns the address of the object in memory in CPython.
If the two variables refer to the same object, the
id() function will produce
the same result.
my_first_list = ['a', 'b', 'c'] print(id(my_first_list)) # 👉️ 140311440685376 my_second_list = my_first_list print(id(my_second_list)) # 👉️ 140311440685376 print(id(my_first_list) == id(my_second_list)) # 👉️ True
None value to the
id() function is always going to return the same
result because there is only one instance of
None in a Python program.
print(id(None)) # 👉️ 9817984 print(id(None)) # 👉️ 9817984
You might also see examples online that check for truthyness.
my_var = None # 👇️ checks if variable stores a truthy value if my_var: print('variable is truthy') # 👇️ checks if variable stores a falsy value if not my_var: # 👇️ this runs print('variable is falsy')
However, this is very different than explicitly checking if a variable stores a
None value because there are many other falsy values that are not
All values that are not truthy are considered falsy. The falsy values in Python are:
0(zero) of any numeric type
If you check if a variable is truthy, you are checking if the variable is not
any of the aforementioned falsy values, not just