Check if a Value is Zero or not None in Python

avatar

Borislav Hadzhiev

Wed Jun 15 20223 min read

Check if a Value is Zero or not None in Python #

To check if a value is zero or not None in Python:

  1. Use the double equals == operator to check if the value is equal to 0.
  2. Use the is not operator to check if the value is not None.
main.py
my_var = None # ✅ check if not None if my_var is not None: print('variable does NOT store a None value') # ✅ check if 0 if my_var == 0: print('variable stores a 0 value') # ✅ check if zero or not None if my_var == 0 or my_var is not None: print('value is zero or not None')

The first if statement checks if the variable doesn't store a None value. Notice that we used the is not operator to do that.

In the second if statement, we used the double equals == operator to check if the variable stores 0.

The third if statement checks if the variable stores 0 or not None. The if block would run if either of the conditions is met.

You should use the is not operator when you need to check if a variable doesn't store a None value.

When we use is or is not, we check for the object's identity.

The pep 8 style guide mentions that comparison to singletons like None should always be done with is or is not, and never the equality operators.

Use the equality operators (equals == and not equals != ) when you need to check if a value is equal or is not equal to another value, e.g. 'a' != 'b'.

Here is an example that better illustrates checking for identity (is and is not) vs checking for equality (== and !=).

main.py
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.

main.py
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
Notice that the identity check failed. Even though the two lists store the same values, in the same order, they point to different locations in memory (they are not the same object).

When we use the not equals != operator, Python calls the __ne__() method on the object.

That is x!=y calls x.__ne__(y). In theory this method could be implemented in an unpredictable way, so checking for None with the is and is not operators is more direct.

You can use the id() function to get the identity of an object.

main.py
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
The function returns an integer, which is guaranteed to be unique and constant for the object's lifetime.

The 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.

main.py
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

Passing a 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.

main.py
print(id(None)) # 👉️ 9817984 print(id(None)) # 👉️ 9817984

You might also see examples online that check for truthyness and falsyness.

main.py
my_var = None # 👇️ checks if variable stores a falsy value if not my_var: # 👇️ this runs print('variable is falsy') # 👇️ checks if variable stores a truthy value if my_var: print('variable is truthy')
However, this is very different than explicitly checking if a variable doesn't store a None value because there are many other falsy values that are not None.

All values that are not truthy are considered falsy. The falsy values in Python are:

  • constants defined to be falsy: None and False.
  • 0 (zero) of any numeric type
  • empty sequences and collections: "" (empty string), () (empty tuple), [] (empty list), {} (empty dictionary), set() (empty set), range(0) (empty range).

If you check if a variable is falsy, you are checking if the variable is any of the aforementioned falsy values (not just None).

Use the search field on my Home Page to filter through my more than 1,000 articles.