In this Python tutorial, you will learn how to Check the variable is None in Python. We will explore multiple examples with different scenarios to check the variable is Not in Python.
While working on a Python project, I needed to check whether the variable contained a None value or not null value before using it. So, I found 4 different solutions to check if var contains None in Python.
I thought I would share it with you, so you can also use those approaches in this type of situation in Python.
4 Ways to Check If Variable is None
Let’s understand the scenario with one practical example,
name = "Peter"
age = None
email = "peter@example.com"
These are the details of the user, but I need to check if any data contains a None value or not, and it will give output like this,
Age contains None Value
Let’s understand all the approaches to check if the variable is None in Python
Python Check If None Using is Operator
First, we will use the “is” operator with None, which is known as the identity operator in Python. This operator checks the equality between two values.
So, we will use this operator to check if the variable contains no value or not.
Syntax
if "value" is None:
- if “value” is None: We will check whether the value is None or not using this syntax in Python.
name = "John"
age = None
email = "john@example.com"
if name is None:
print("Name is required")
elif age is None:
print("Age is Required")
elif email is None:
print("Email is Required")
In the above code, we have client data where name and email have a value, but age is None like this: age = None, so we need to give a message to the client that field name is required.
Here, we are using the “is” operator to check the equality of user input and None value like this: elif age is None. If any of the conditions is True, which means any of these fields contain a None value, then it will return the given message.
Python None Check Using == Operator
We can also use the “==” operator, which is a part of the comparison operator in Python. This operator will compare two values and return True or False based on their equality.
So, we will use the == operator to check whether the variable contains a None value or not.
Syntax
if var_name == None
- if var_name == None: Here we are comparing input value with None, if variable is none then it will print True.
name = "John"
age = None
email = "john@example.com"
if name == None:
print("Name is required")
elif age == None:
print("Age is Required")
elif email == None:
print("Email is Required")
In the above code, we are using the same example but with a different approach. Here, we are using the == comparison operator to compare the user data with the None value; if it returns True, then print the alert message that the field is required.
As a result, when you don’t provide the Age value, it shows ‘Age is Required’.
Python Check If Variable is None Using not Operator
We can also use the Not Operator to check whether the variable contains a None value. The Not operator reverses the condition’s result. Suppose the result is True; then, in Python, the Not operator will return False.
Syntax
if not var_name:
- if not var_name: Here, if var_name is None, it will return True and go to the statement block.
name = "John"
age = 45
email = None
if not name :
print("Name is required")
elif not age :
print("Age is Required")
elif not email :
print("Email is Required")
In the above code, we have user data with static values, but the email is none, so we need to give the message that email is required. Here, we are using the keyword because when you give if email: which means it will check whether the data is there or not and return True.
But we are using a not with an if condition like this: elif not email: so if the data is not there, then it will return True and execute that statement block.
Python Check Variable is None Using the isinstance() Method
Here we will use the isinstance() method, which is a built-in function in Python. This method returns a Boolean value( True or False) based on the given parameters. The first parameter takes an object like a number, list, string, etc., and the second parameter will be the type of data.
If the type of the given object in the first and second parameters match, then it will return True.
Syntax
isinstance(object, type(None))
- isinstance(object, type(None)): This syntax will return True or False based on the value is None or not.
flight_A = "On time"
flight_B = None
flight_C = "Delay"
if isinstance(flight_A, type(None)):
print("Status for Flight A not updated.")
elif isinstance(flight_B, type(None)):
print("Status for Flight B not updated.")
elif isinstance(flight_C, type(None)):
print("Status for Flight C not updated.")
else:
print("Every flight is updated")
In the above code, we have updates on the flights, but some flight data is not updated, so we need to give a message to the team that the Status of that flight is not updated.
So here we are using the isinstance() method, which is like this: elif isinstance(flight_B, type(None)). Here, the condition returns True because flight B is None.
Conclusion
In this Python article, you learned How to check whether the variable contains a None value or not using different methods like isinstance() and different operators like “is,” “==” and not operator in Python.
You may also like:
- Check if the variable exists in Python
- How to Create a String with Variables in Python [Create a String with Multiple Variables in Python]
- Python write variable to file [4 Methods]
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.