How to Check if a Variable is None in Python?

Recently, I was reviewing some of my old Python projects, and I noticed that I had used several different ways to check if a variable was None.

At first, this seemed like a small detail. But then I realized that choosing the right method can make your code easier to read, faster to debug, and less error-prone.

In this tutorial, I’ll share with you five simple ways I use to check if a variable is None in Python. These are methods I’ve applied in real-world projects over my 10+ years of writing Python code.

Why Do We Need to Check for None in Python?

When I work with Python, I often use None as a placeholder value. It represents the absence of a value or a null state.

For example, if I’m fetching data from an API in the USA and the API doesn’t return a value, I might get None. Before I process that data, I need to check if the variable actually contains something useful.

Without this check, my program could throw errors or behave unpredictably.

Method 1 – Use the is Operator in Python

The most Pythonic way to check if a variable is None is by using the is operator. The is operator checks for identity, not equality. This means it checks whether two objects point to the same memory location.

user_age = None

if user_age is None:
    print("The variable user_age is None.")
else:
    print("The variable user_age has a value.")

I executed the above example code and added the screenshot below.

python check if none

In this example, the variable user_age is None. The is operator correctly identifies it. I use this method almost every day because it’s simple, clean, and easy to understand.

Method 2 – Use Python’s is not Operator

Sometimes, I want to check if a variable is not None. In that case, I use the is not operator. This is especially useful when I need to ensure a variable has a valid value before I perform operations on it.

user_name = "John"

if user_name is not None:
    print("The variable user_name is not None.")
else:
    print("The variable user_name is None.")

I executed the above example code and added the screenshot below.

python if none

Here, user_name contains "John", so the condition evaluates to True. I find this method handy when I’m validating user inputs in Python applications.

Method 3 – Use an if Statement (Truthy/Falsy Check)

Another quick way is to use a simple if condition. In Python, None is considered falsy, which means it evaluates to False in a boolean context.

product_price = None

if product_price:
    print("The variable product_price has a value.")
else:
    print("The variable product_price is None or empty.")

I executed the above example code and added the screenshot below.

python is none

This method works, but I usually avoid it when I specifically want to differentiate between None, 0, or an empty string.

For example, in the USA, product prices can be 0 (like free samples), and I don’t want to confuse that with None.

Method 4 – Use type() for Explicit Checking

Sometimes, I like to be very explicit in my code. In those cases, I check the type of the variable.

city = None

if type(city) is type(None):
    print("The variable city is None.")
else:
    print("The variable city has a value.")

I executed the above example code and added the screenshot below.

python if not none

This is not as common, but it’s useful when I’m debugging complex Python applications and I want to be 100% clear.

Method 5 – Use try/except to Handle None

In real-world Python projects, I sometimes don’t just want to check for None; I want to handle it gracefully.

user_zipcode = None

try:
    print("The length of zipcode is:", len(user_zipcode))
except TypeError:
    print("The variable user_zipcode is None, so it has no length.")

Here, instead of crashing, the program catches the error and prints a friendly message.

This approach is especially useful when I’m working with external data sources in the USA, such as ZIP codes or addresses, where missing values are common.

Best Practices for Checking None in Python

Over the years, I’ve learned a few best practices that make working with None easier:

  • Always prefer is or is not over == or !=.
  • Be cautious when using truthy/falsy checks, especially if 0 or empty strings are valid values.
  • Use try/except blocks when you’re not sure if a variable might be None and you’re performing operations on it.
  • Keep your checks simple and readable. Code readability matters a lot in Python.

Real-World Example: Checking API Response in Python

Let me share a practical example from a project I worked on. I was building a Python application that fetched weather data for different cities in the USA.

Sometimes, the API would return None if the city name was invalid. I needed to check for None before processing the response.

weather_data = None  # Simulating an API response

if weather_data is None:
    print("No data received from the weather API.")
else:
    print("Processing the weather data...")

This simple check saved me from multiple runtime errors and made the application more reliable.

Wrapping Up

So those are the five simple methods I use to check if a variable is None in Python.

  • Use is for the most Pythonic check.
  • Use is not when you want to ensure the variable has a value.
  • Use truthy/falsy checks carefully.
  • Use type() for explicit debugging.
  • Use try/except when working with operations on variables that might be None.

When you work with Python daily, these checks become second nature. They help you write clean, professional, and bug-free code.

You may also read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.