Python Null | What is Null in Python | None in Python

What is Null in Python?

In Python, there is no null keyword or object available. Instead, you may use the β€˜None’ keyword, which is an object.

We can assignΒ NoneΒ to any variable, but you can not create other NoneType objects.

Note:

  • We can define None to any variable or object.
  • None is not equal to 0
  • In addition, None is not equal to FALSE
  • None is not equal to an empty string
  • None is only equal to None

Note: All variables that are assigned None point to the same object. New instances of None are not created.

Syntax

The syntax of None statement is:

None

This is how you may assign the β€˜none’ to a variable in Python:

none = None

We can check None by keyword β€œis” and syntax β€œ==”

Null Object in Python

In Python, the ‘null‘ object is the singleton None.

The best way to check things for “Noneness” is to use the identity operator, is:

if foo is None:
    ...

Note: The first letter in β€˜None’ keyword is the capital N. The small β€˜n’ will produce an error.

In other programming languages, for example, this is how you may create a null variable in PHP and Java.

Null in Java

myVariable = null;

Null in PHP

$myVariable = NULL;

If you need to evaluate a variable in the if condition, you may check this as follows in Java:

if(myVariable == null) {
  System.out.println(”Some output”);
}

How to use the β€˜None’ in Python. I will use it in the if statement and a few compound data types.

Checking if a variable is None (Null) in Python using is operator:

# Declaring a None variable
var = None

if var is None: # Checking if the variable is None
  print("None")
else:
  print("Not None")

Python Null Using the == operator

Rather than using the identity operator in the if statement, you may also use the comparison operators like ==, != etc. for evaluating a β€˜none’ value. See the example with the code below where the same code is used as in the above example except the comparison operator:

#A demo of none variable with == operator

ex_none = None

if ex_none == None:
    print('The value of the variable is none')
else:
    print('Its a not null variable')

The output of the above example is:

The value of the variable is none

None (Null) value in a List example

Similarly, you may use the Null value in the compound data type List. Just use the None keyword as in case of sets.

See this example where I have created a list and the third item in the list is None keyword:

# An example of a List with None

str_list_None = ['List','with', None, 'Value']

for str in str_list_None:
    print ("The current list item:",str)

Using None(Null) for default args in Python

Using anything but None for default, arguments make objects and functions less extensible.

We’re going to useΒ NoneΒ for our default argument, subbing in our default value when none is provided:

DEFAULT_FOOD = 'catfood'

class Cat:
    def eat(self, food=None):
        if food is None:
            food = DEFAULT_FOOD
        return food

cat = Cat()
cat.eat()  # catfood
cat.eat('tuna')  # tuna

Our class still behaves the same but is easier to extend. Look how we no longer need to worry about the default value constant in our Tabby to maintain the same method signature:

Check the type of None (Null) object:

# Declaring a variable and initializing with None type
typeOfNone = type(None) 

print(typeOfNone)

Check if None is equal to None

# Python program to check None value

# initialized a variable with None value
myval = None

if None is None:
    print('None is None')
else:
    print('None is Not None')

print(None == None)

Output:

None is None
True

Check if None is equal to an empty string

# Inilised a variable with None value
myval = ''

if myval == None:
    print('None is equal to empty string')
else:
    print('None is Not Equal to empty string')

print(None == myval)

Output

None is Not Equal to empty string
False

Also Read: Learn Python the Hard Way Review

Assigning a NULL value to a pointer in python

In Python, we use None instead of NULL. As all objects in Python are implemented via references, See the code below:-

class Node:Β 
Β Β def __init__(self):Β 
Β Β Β Β self.val = 0
Β Β Β Β self.right = None
Β Β Β Β self.left = Non

None cannot be overwritten

In a much older version of Python (before 2.4) it was possible to reassign None, but not anymore. Not even as a class attribute or in the confines of a function.

# In Python 2.7
>>> class SomeClass(object):
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: cannot assign to None
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to None

# In Python 3.5
>>> class SomeClass:
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: invalid syntax
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to keyword

It’s, therefore, safe to assume that all None(Null) references are the same. There’s no “custom” None.

Difference between Null and empty string in Python

Python doesn’t have a Null string at all.

Python does have a None value but it isn’t a string it is A None that can be applied to all variables – not just those which are originally defined as a string. In fact, Python doesn’t care what a variable has been defined as previously – this is entirely valid code (although not a great idea):

data=23
data = [23,24,25,26,27] # Now a list
date = β€˜Hello World’ # Now a string
data = None

So to try to answer your question

This is a string :

data =β€˜Hello World’

This is an Empty string :

data =β€˜β€™

And this is None

date = None

A Null or empty string means that there is a string but its content is empty – 

len(β€˜ ’) ==0. For Python, the term β€˜Empty string’ is preferred.

Image Explains it all

This image shows a clear difference between Non-Zero, 0, null and undefined.

Difference between Null and None

Conclusion

None has a distinctive status in Python language. It’s a preferred baseline value because many algorithms treat it as exceptional.

In such scenarios, it can be used as the flag to signal that the condition requires some special handling, such as setting the default value.

If you didn’t find what you were looking for, then do suggest us in the comments below. We will be more than happy to add that.

Subscribe
Notify of
guest
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
elijah.ho
elijah.ho
2 years ago

It’s useful to me

Pratik Kinage
Admin
2 years ago
Reply to  elijah.ho

I’m glad it helped you πŸ™‚

Regards,
Pratik