Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
none
null in python

What is the None keyword in Python?

Educative Answers Team

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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 ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring