How to Check if a Variable is None in Python

In any programming language, a variable is used to store any input data. Any type of data will be stored in a Python variable like string, integer, list, tuple, etc. The “None” value in Python has a specific location in memory, which does not mean that the None value equals “0” or Nothing. None is simply a value stored in some specific location in memory.

In this post, we will demonstrate numerous methods to check the None variable in Python. Let’s explore them one by one:

How to Check if a Variable is None in Python?

The variable value will be “None” when we store any variable in the location of “None”. To check any None variable in Python, “is operator”,  “isinstance method” and “Try-Except” functions are used. We use the “None” value when we define a Null value for any variable. The none value is equal to its own type named “Nonetype”.

Let’s understand the “None” variable using the following example.

xyz = None
print(type(xyz))
how to handle linux files and direc...

In the above code:

  • We initialized a None value to a variable named “xyz”.
  • We print the variable type with the help of the “type()” function.
<class 'NoneType'>

The type of variable “xyz” have printed with the help of the “type()” function.

Method 1: Using the “is” operator

In Python, the “is” and “is not” operator is used to checking the None Variable. The “is” operator function looks similar to the “==” equality operator, but there is a difference between them while executing. The “is” operator will only identify the value if the value is exactly in the same location in memory, but the equality operator compares two values and reports the equality of the values.

Let’s understand the working of “is” and ”is not” operator via the following example:

Code

#using is operator
var_value = None
int_value = 5

if(var_value is None):
   print("The var_value is None")

if(int_value is not None):
   print("The int_value is not None")

In the above code:

  • The two different variables have been initialized with a value “None” in variable “var_value” and an integer value “5” in “int_value”.
  • To check the “none value” in both of the variables, if statement is used along with the “is” and “is not” operators.
  • If the variables have a “none” value, then it will print “The var_value is None” and if the value is not equal to “none” then the program prints “The int_value is not None”.

Output

The above output has shown that the “var_value” is “None” and “int_value” is not “None”.

Method 2: Using the isinstance() Method

Another built-in method named “isinstance()” is used to check the “None” variable in Python. In this method, we compare the two values by passing the argument in the parenthesis of the “isinstance()” function. The first argument contains variable names, and the second contains the data type we want to compare.

Let’s understand the “isinstance()” method using the following example:

Code

#using isinsistance() method

var_value = None
float_value = 5.92

print(isinstance(var_value, type(None)))
print(isinstance(float_value, type(None)))

In the above code:

  • Two variables named “var_value” and “float_value” are initialized.
  • The “isinsistance()” method is used to check the “None” value of initialized variables by comparing it with its none type.
  • If the variable’s value is “None” then the boolean value True will return in output otherwise, False.

Output

The output has returned a True value when a variable is None and a False value when the variable will Not equal to None.

Method 3: Using Try-Except Block

Try except block is also used to check none variable in python. Try block is used when we want to test the code for error, and except block is used to handle that error. We can easily check the “None” variable with the help of this method. This method is suitable and can mostly use for error exception handling in Python.

Let’s understand the “Try-Except” method using the following example:

Code

#using try-except block
var_value = None
dec_val = 92.323

try:
   sum = var_value + list_val
except:
   print("The value of one Variable is None")

The code is described as:

  • Two variables named “var_value” and “dec_val” are initialized.
  • The “try” block is used to sum these two variables. If the “try” block gives an error, the value statement written in the “except” block will be printed.

Output

The output has run the “except” block due to the presence of the None value in the given variables.

Conclusion

In Python, the “is operator”, “isinstance()” method, and “Try-Except Block” is practiced here to check a “None” variable. The most convenient method to check the None type variable is the “is” and “is not” operators. The “isinstance()” method checks the variable type by comparing it with the user “input data type” and returns the “True” or “False”. This post has demonstrated numerous methods to find a Non-type variable in Python.