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.
None is a singleton, meaning there is only one instance of it in a Python program.bool(None) == False).None to VariablesUsed to initialize a variable that may later be assigned a meaningful value.
a = None
NoneSince 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")
Functions that do not explicitly return a value will return None.
def do_nothing():
pass
result = do_nothing()
print(result) # Output: None
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!
is or is not for None checks (not equality operators).None as a default mutable argument (use immutable alternatives if needed).