
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
The Null Object in Python
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'
- Related Articles
- JavaScript/ Typescript object null check?
- Set JavaScript object values to null?
- How to implement Null object Pattern in C#?
- Filter away object in array with null values JavaScript
- Calling a member function on a NULL object pointer in C++
- Python Pandas - Return the frequency object from the PeriodIndex object
- Python Pandas – Propagate non-null values backward
- Python Pandas – Propagate non-null values forward
- Python - Remove a column with all null values in Pandas
- In MySQL what is the difference between != NULL and IS NOT NULL?
- Explain briefly the Object oriented concepts in Python?
- Python - How to drop the null rows from a Pandas DataFrame
- Python Pandas – Check for Null values using notnull()
- Object Oriented Programming in Python?
- Python object serialization
