Trusted answers to developer questions

What is the None keyword in Python?

Free System Design Interview Course

Many candidates are rejected or down-leveled due to poor performance in their System Design Interview. Stand out in System Design Interviews and get hired in 2024 with this popular free course.

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.

svg viewer

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 TAGS

python
none
null in python
Copyright ©2024 Educative, Inc. All rights reserved
Did you find this helpful?