Related Tags

What is the None keyword in Python?


The None keyword is used to define a null variable or an object. In Python, None keyword is an object, and it is a data type of the class NoneType.

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

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

Note: None statement supports both is and == operators.

Interesting Facts

  • None is not the same as False.
  • None is not 0.
  • None is not an empty string.
  • Comparing None to anything will always return False except None itself.

Examples


  1. Checking if a variable is None 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")

  1. Checking if a variable is None using == operator:
# Declaring a None variable
var = None

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

  1. Check the type of None object:
# Declaring a variable and initializing with None type
typeOfNone = type(None) 

print(typeOfNone)

  1. Comparing None with None type:
# Comparing None with none and printing the result
print (None == None)

  1. Comparing None with False type:
# Comparing none with False and printing the result
print(None == False)

  1. Comparing None with empty string:
# Declaring an empty string
str = ""
# Comparing None with empty string and printing the result
print (str == None)
RELATED COURSES
View all Courses