None in Python – A Brief Introduction to the None Keyword

Filed Under: Python
None in python

None in Python is used for defining null variables and objects. None is an instance of the NoneType class.

This class is a singleton, meaning that there can be only one instance of it. In fact, all the variables assigned to none, point to the same object in Python.

Working with None in Python

We will verify this later in the tutorial and learn lots more about None in Python.

1. Declaring and Assigning None in Python

Let’s start by declaring a variable and assigning it to None. Run the following line of code in your python notebook.

a = None

Now we can go ahead and print the value of the variable.

print(a)

Output :

None

We get ‘None’ as the output.

2. Printing the ‘Type’ of a variable assigned to None in Python

What do you think should be the type of a variable assigned to None?

Let’s print it out using the following line of code :

a = None
print(type(a))

We get the output as :

<class 'NoneType'>

This is in accordance with what we discussed above. A variable assigned to None in Python points to an instance of ‘NoneType’ class.

There is only one instance of the NoneType class, therefore all the variables set to null point to the same object.

In fact, we can compare the IDs of two different variables set to None.

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

Output :

10306432
10306432
10306432

We can see that None, a and b have the same object IDs.

3. Comparing a variable set to None with None

Let’s use the ‘==‘ and ‘is‘ operator to compare a variable set to None with None.

a = None
print(a==None)
print(a is None)

Output :

True
True

You can also use None as a condition in if statements. Let’s see how to do that.

4. Using None in Python with Conditional Statements

Let’s use variables set as None in If conditions.

a = None
if (a) :
  print("Condition is true")
else :
  print("Condition is false")

Output :

Condition is false

We can see that a variable set as None works as false. We can use this to check for variables that are set to None in our programs.

You can also use the condition with a ‘not‘.

a = None
if (not a) :
  print("Not none means true")

Output :

Not none means true

5. Returning None from a function

When we don’t set a return value for a function then None is the default return type.

We can verify this with the following piece of code :

def fun() :
 print("Hello World")

a = fun()
print(a)
print(id(a))
print(type(a))

Output :

Hello World
None
10306432
<class 'NoneType'>

The ID in the output is same as the IDs that were printed above.

Conclusion

This brief tutorial was about None in Python. We established that all variables set to None point to the same instance of NoneType class. We also covered how to use None datatype as a condition in If statements. Hope you had fun learning with us!

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages