• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Check if Variable is None in Python

Check if Variable is None in Python

August 30, 2022 Leave a Comment

To check if a variable is equal to None, you can use the equality operator == and check if the variable is equal to None.

a = None
b = "Not None"

print(a == None)
print(b == None)

#Output:
True
False

You can also use the is keyword to check if a variable is None.

a = None
b = "Not None"

print(a is None)
print(b is None)

#Output:
True
False

When working with variables in Python, the ability to check if a variable is a specific type easily is valuable.

One such case is if you want to check if a variable is None.

The None keyword is used to define a null value. None is of type NoneType and only None can be None.

None is different than 0, False, and an empty string.

You can use None to set a variable to null and another case where you might get None is if you have a function which doesn’t return a value.

To check if a variable is equal to None, you can use the equality operator == and check if the variable is equal to None.

Below shows you an example of how you can check if a variable is None in Python.

a = None
b = "Not None"

print(a == None)
print(b == None)

#Output:
True
False

You can also use the is keyword to check if a variable is None.

a = None
b = "Not None"

print(a is None)
print(b is None)

#Output:
True
False

Hopefully this article has been useful for you to learn how to check if a variable is none or not.

Other Articles You'll Also Like:

  • 1.  PROC LIFETEST Equivalent in Python
  • 2.  Using Python to Find Second Smallest Value in List
  • 3.  Length of Set Python – Get Set Length with Python len() Function
  • 4.  Clear File Contents with Python
  • 5.  Convert Set to List in Python
  • 6.  Run Something Every 5 Seconds in Python
  • 7.  Using Python to Iterate Over Two Lists
  • 8.  Sort List of Tuples in Python
  • 9.  Python power function – Exponentiate Numbers with math.pow()
  • 10.  Find Index of Maximum in List Using Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy