What is NoneType in Python and How to Use It

In Python, there is no null keyword, but there is None.

None is the function’s return value that “doesn’t return anything”.

None is often used to represent the absence of a value, as default parameters are not passed to the function. You can not assign a null value to the variable; if you do, it is illegal and will raise a SyntaxError.

data = null


Traceback (most recent call last):
  File "/Users/krunal/Desktop/code/pyt/database/app.py", line 1, in <module>
    data = null
NameError: name 'null' is not defined

Keypoints of None in Python

  1. Comparing None to anything will always return False except None itself.
  2. None is not a 0.
  3. None is not an empty string.
  4. None is not the same as False.

Python NoneType

NoneType is a type of a None Object in Python. The None keyword is an object, a data type of the class NoneType. We can assign None to any variable, but we can not create other NoneType objects.

NoneType is simply the type of the None singleton.



<class 'NoneType'>

To check the variable data type in Python, use the type() method.

If the Python regular expression in the re.search() does not match, it returns the NoneType object.

Checking if a variable is None

To check whether a variable is None, use the is operator in Python. With the is operator, use the syntax object is None to return True if the object has the type NoneType and False otherwise.

data = None

if data is None:
    print("It is in fact a None")
    print("No, it is Not None")


It is in fact a None

You can see that is operator returns True because the data is None, and hence if condition returns True and execute its body, which will print the “It is a None“.

Comparing None with False type

You can compare the None with the False value, but it returns False since False and None are different.

data = None

print(data == False)



The None keyword is also used for matching or identifying whether a specific function returns any value.

TypeError: ‘NoneType’ object is not iterable

For an object to be iterable in Python, it must include a value. A None value is not iterable because it holds no values or objects. This is because none represents the null value in Python.

There is a difference between a None object and an empty iterable. The ‘NoneType’ object is not an iterable error and is not generated if you have any empty list or a string.

Technically, you can prevent the NoneType exception by checking if a value is equal to None using is operator or == operator before you iterate over that value.

To solve the NoneType error, ensure that any values you try to iterate over should be assigned an iterable object, like a string or a list.

That’s it.

Related posts

Python check None



Python check nan

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.