Python NULL – How to identify null values in Python?

Python Null None

In many programming languages, ‘null’ is used to denote an empty variable or a pointer that does not point to anything. ‘null’ is basically equal to 0. Whereas in Python, there is no ‘null’ keyword available. Instead, ‘None’ is used for this purpose, which is an object.

This tutorial will provide you with a comprehensive guide on null in Python and we will also learn how to declare a null variable in Python. Additionally, we will also see how to check if a variable is None. Let’s start with an introduction to null in Python.

Null in Python

Null is Python practically does not exist, it uses None instead. Whenever a function doesn’t have anything to return i.e., it does not contain the return statement, then the output will be None.

In simpler words, the None keyword is used to define a null variable or null object.

def func_no_return():
    a = 5
    b = 7
print(func_no_return())

Output:

None

NOTE: Whenever we assign None to a variable, all the variables that are assigned to it point to the same object. No new instances are created.

Data Type of None

In Python, unlike other languages, null is not just the synonym for 0, but is an object in itself.

We can use the type() method which returns the class type of the object passed to it. It accepts only one argument. To see the None type, we pass None as an argument to this method.

print(type(None))

Output:

<class 'NoneType'>

In the above output, we can see that type() method returns <class 'NoneType'> which means, None is an object in Python, and the data type of the class is NoneType.

Declaring null variables in Python

Null variables in Python are not declared by default. That is, an undefined variable will not be the same as a null variable. To understand, all the variables in Python come into existence by assignment only. Have a look at the code below:

Code Example

The above code shows the difference between an undefined variable and a None variable.

How to check if a variable is None in Python?

You can check whether a variable is None or not either using the is operator or the == operator as shown below

Using the ‘is’ operator

#declaring a None variable
a = None

if a is None:                   #checking if variable is None
    print("None")
else:
    print("Not None")

Here we are checking if a is none which returns true then print “None”, returns false then prints “Not None”.

Output:

None

We got None, which means the ‘is” operator successfully identified it.

Using the ‘==’ operator

#declaring a None variable
a = None

if (a == None):                   #checking if variable is None
    print("The value of the variable is None")
else :
    print("The value of variable is Not None")

Here we are checking if a is None using the equality operator. If the equality operator returns true then print “The value of the variable is None”, else print “The value of variable is Not None”.

Output:

The value of variable is None

Here we got the expected result.

Conclusion

In this tutorial, we have learned that the None keyword is used to define a null variable, None is of immutable type and can be used to mark missing values and default parameters. We have also learned to check if a variable is None in Python using the ‘is’ operator and the ‘==’ operator, which can be used accordingly. Hope you find this tutorial useful.

Reference

https://stackoverflow.com/questions/19473185/what-is-a-none-value