Python’s None and Constants Explained | Variables Guide
Learn how Python’s built-in None constant works and how to define your own constants. Explore identity checks (is None vs == None), naming conventions, and best practices.
In Python, None is a special constant that represents the absence of a value or a null value. It is an object of its own datatype, called NoneType.
Key Characteristics
None is a singleton, meaning there is only one instance of it in a Python program.
It is falsy in Boolean contexts (bool(None) == False).
Often used as a default argument or a placeholder for optional or missing values.
Common Use Cases
1. Assigning None to Variables
Used to initialize a variable that may later be assigned a meaningful value.
a = None
2. Checking for None
Since None is a singleton, use is or is not for identity comparison (not ==).
if a is None: print("a is None") else: print("a is not None")
3. Default Return Value in Functions
Functions that do not explicitly return a value will return None.