What is the None keyword in Python?

In Python, None is a special keyword that represents the absence of a value. It is used to signify that a variable or object does not have a value assigned to it. In other words, it is a way to represent "nothing" or "null" in Python.

Use of None

  1. When you create a variable without assigning any value, it is automatically initialized to None.
  2. Functions that do not explicitly return a value return None by default.
  3. It can be used as a placeholder when you want to define a variable or object initially without assigning a specific value.
Example
my_variable = None print(my_variable) # Output: None
Example
def do_something(): # Function with no return statement pass result = do_something() print(result) # Output: None

Comparison with None

You can use the is or is not operators to compare variables with None to check if they are holding the None value.

Example
x = None y = 5 if x is None: print("x is None.") else: print("x is not None.") if y is not None: print("y is not None.") else: print("y is None.")
Output: x is None. y is not None.

Returning None from Functions

If a function has no explicit return statement or returns without a value, it automatically returns None. It is common to use None as a return value to indicate that a function has no meaningful result to return.

Example
def divide(a, b): if b == 0: return None # Returning None to indicate division by zero else: return a / b result1 = divide(10, 2) result2 = divide(10, 0) print(result1) # Output: 5.0 print(result2) # Output: None

In this example, the divide function returns None when the divisor is 0, indicating that division by zero is not allowed.

Python NoneType

None is a unique object and the sole instance of the class NoneType. Attempts to create additional instances of NoneType will always return the same None object, making it a singleton. This distinct characteristic of None ensures that there is only one instance of this object throughout the program's execution. As a favored baseline value, None is frequently employed to represent the absence of a meaningful value in various algorithms and scenarios within the language and library. Its usage as an exceptional value allows for concise and expressive code, providing a clear indication of missing or unspecified information.

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 is no "null" keyword like in some other programming languages. Instead, Python uses the None object to represent the absence of a value. The None object is a singleton, meaning there is only one instance of it throughout the program's execution. You can verify its uniqueness by using Python's identity function id(), which returns a unique number assigned to each object. If the id of two variables is the same, it confirms that they both point to the same None 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.

Conclusion

The use of None instead of "null" is a design choice in Python, and it serves as a clear indication of missing or undefined values. By using the identity function id() to check object uniqueness, developers can ensure that variables indeed refer to the same None object, providing a reliable way to handle absence of values in Python programs.