Python does not have a null object. But the most closely related similar object is none. In this article, we will see how how None behaves in Python.
Checking the type of Null and None we see that there is no Null Type and the None object is of type NoneType.
Example
print(type(None)) print(type(Null))
Output
Running the above code gives us the following result −
Traceback (most recent call last): File "C:\Users\xxx\scratch.py", line 4, in print(type(Null)) NameError: name 'Null' is not defined
Key facts about None
None is the same as False.
None is the same as False.
None is the same as False.
None is an empty string.
None is 0.
Comparing None to anything will always return False except None itself.
Null Variables in Python
An undefined variable is not the same as a Null variable. A variable will be null in Python if you assign None to it.
Example
var_a = None print('var_a is: ',var_a) print(var_b)
Output
Running the above code gives us the following result −
Traceback (most recent call last): File "C:\Users\Pradeep\AppData\Roaming\JetBrains\PyCharmCE2020.3\scratches\scratch.py", line 5, in <module> print(var_b) NameError: name 'var_b' is not defined var_a is: None
None not associated with methods
If something is declared as None, you can not use any methods to add, remove elements from it.
Example
listA = [5,9,3,7] listA.append(18) print(listA) listA = None listA.append(34) print(listA)
Output
Running the above code gives us the following result −
[5, 9, 3, 7, 18] Traceback (most recent call last): File "C:\Users\Pradeep\AppData\Roaming\JetBrains\PyCharmCE2020.3\scratches\scratch.py", line 7, in listA.append(34) AttributeError: 'NoneType' object has no attribute 'append'