Lesson 3of 7
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
Noneis 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.
def do_nothing():
pass
result = do_nothing()
print(result) # Output: None 4. Optional Function Arguments
Used to indicate that an argument is optional.
def greet(name=None):
if name is None:
print("Hello, Guest!")
else:
print(f"Hello, {name}!")
greet() # Output: Hello, Guest!
greet("Alice") # Output: Hello, Alice! Best Practices
- Use
isoris notforNonechecks (not equality operators). - Avoid using
Noneas a default mutable argument (use immutable alternatives if needed).