What is the None keyword in Python?

None has a special status in Python. The None is used to define a null variable or an object, and it is a data type of the class NoneType .
>>> x = None >>> type(x) <class 'NoneType'> >>>

Python NoneType


Python None Keyword
None is the sole instance of the class NoneType and any further attempts at instantiating that class will return the same object, which makes None a singleton (meaning there is only one None). It's a favourite baseline value because many algorithms treat it as an exceptional value, used in many places in the language and library to represent the absence of some other value .

Checking if a variable is None using is operator:


Python None

Assigning a value of None to a variable is one way to reset it to its original, empty state. It is not the same as 0, False, or an empty string. None is a data type just like int, float, etc. and only None can be None. You can check out the list of default types available in Python in 8.15. types — Names for built-in types.

So, let's check how Python None implemented internally.
>>> dir(None) ['__bool__', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__form at__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__se tattr__', '__sizeof__', '__str__', '__subclasshook__'] >>>
  1. Except __setattr__, all others are read-only attributes. So, there is no way we can alter the attributes of None.

Checking if a variable is None using == operator:


Understand Python Null and None Keyword

Python Null and None Keyword

There's no null in Python ; instead there's None. You can check None's uniqueness with Python's identity function id(). It returns the unique number assigned to an object, each object has one. If the id of two variables is the same, then they point in fact to the same object .
NoneType = type(None) print(id(None)) my_none = NoneType() print(id(my_none)) another_none = NoneType() print(id(another_none)) def foo(): pass return_value = foo() print(id(return_value))
output
1665592064 1665592064 1665592064 1665592064

Comparing None with False type:

>>> print(None==False) False

Interesting Facts about Python None

  1. None is an instance of NoneType.
  2. We cannot create other instances of NoneType.
  3. None is not the same as False.
  4. None cannot have new attributes.
  5. Existing attributes of None cannot be changed.
  6. None is not an empty string.
  7. None is not 0.
  8. If the function doesn't return anything that means its None in Python.
  9. We cannot even change the reference to None by assigning values to it.
  10. Comparing None to anything will always return False except None itself.