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 ofNone
are not created.
The syntax of None
statement is:
None
Note:
None
statement supports bothis
and==
operators.
None
is not the same as False
.None
is not 0.None
is not an empty string
.None
to anything will always return False
except None
itself.
None
using is
operator:
# Declaring a None variablevar = Noneif var is None: # Checking if the variable is Noneprint("None")else:print("Not None")
None
using ==
operator:
# Declaring a None variablevar = Noneif var == None: # Checking if the variable is Noneprint("None")else:print("Not None")
None
object:
# Declaring a variable and initializing with None typetypeOfNone = type(None)print(typeOfNone)
None
with None
type:
# Comparing None with none and printing the resultprint (None == None)
None
with False
type:
# Comparing none with False and printing the resultprint(None == False)
None
with empty string
:
# Declaring an empty stringstr = ""# Comparing None with empty string and printing the resultprint (str == None)