Python None
None
None is a special constant in Python that represents the absence of a value. It means "nothing here", "empty", or "not set yet".
middle_name = None
This tells anyone reading the code that middle_name exists, but deliberately has no value.
None Is Not 0, False, or an Empty String
It is important to understand what None is not:
| Value | Meaning |
|---|---|
None | No value at all |
0 | A number whose value is zero |
False | A Boolean value meaning "not true" |
"" | A string that exists but contains no characters |
[] | A list that exists but contains no items |
For example, a shopping cart total of 0 means "the total is zero", while a total of None means "the total has not been calculated yet". These are different situations, and None lets your program tell them apart.
print(None == 0)
print(None == False)
print(None == "")
Expected output:
False
False
False
NoneType
None belongs to its own data type, called NoneType. There is exactly one value of this type in Python: None itself.
value = None
print(type(value))
Expected output:
<class 'NoneType'>
Because there is only one None object, every variable set to None refers to the same object. An object with only one instance is called a singleton.
Note:
Noneis a keyword and is case-sensitive.noneorNULLraise aNameError. (Other languages usenullornilfor a similar idea.)
Assigning None to a Variable
Developers assign None when a value is unknown, optional, or will be provided later.
Example: Assigning and Printing None
status = None
print(status)
Expected output:
None
Practical Example: Value Set Later
winner = None
scores = {"Asha": 82, "Ravi": 95, "Meena": 88}
for name, score in scores.items():
if score > 90:
winner = name
print("Winner:", winner)
Expected output:
Winner: Ravi
winner starts as None, meaning "no winner found yet". If no score were above 90, it would remain None, and the program could check for that.
Comparing Values With None
When checking for None, always use the identity operators:
isis not
if value is None:
...
Avoid == None. Because None is a singleton, is checks for exactly that object, which is both correct and slightly faster. The == operator can be redefined by custom classes, so value == None might produce surprising results with some objects. PEP 8, Python's style guide, recommends is and is not for None comparisons.
Example: Using is With None
data = None
if data is None:
print("Data is missing")
else:
print("Data received")
Expected output:
Data is missing
Example: Using is not With None
response = "OK"
if response is not None:
print("Response available")
else:
print("No response")
Expected output:
Response available
The Boolean Value of None
In a Boolean context, such as an if condition, None is treated as False — it is falsy.
result = None
print(bool(result))
Expected output:
False
Why Explicit Checks Are Better
Since many values are falsy (None, 0, "", []), a plain truthiness check cannot tell them apart:
discount = 0
if not discount:
print("No discount provided")
if discount is None:
print("Discount not set")
else:
print("Discount is", discount)
Expected output:
No discount provided
Discount is 0
The first check wrongly reports that no discount was provided, even though a discount of 0 was deliberately set. The explicit is None check correctly distinguishes "not set" from "set to zero". Use is None whenever 0, False, or an empty value is a valid value in your program.
Functions That Return None
If a function has no return statement — or uses return with no value — Python automatically returns None.
Example: A Function Without a Return Value
def show_message():
print("Hello, Python!")
output = show_message()
print(output)
Expected output:
Hello, Python!
None
The function prints its message, then returns None, which is stored in output and printed.
A Common Mistake: Methods That Return None
Many methods that modify an object in place — such as list.sort() and list.append() — return None:
numbers = [3, 1, 2]
numbers = numbers.sort()
print(numbers)
Expected output:
None
sort() sorted the list but returned None, which then replaced the list. Call numbers.sort() on its own line instead, or use sorted(numbers).
Returning None to Signal "Not Found"
Functions often return None deliberately when they cannot find a result:
def find_user(user_id):
users = {101: "Asha", 102: "Ravi"}
return users.get(user_id)
user = find_user(105)
if user is None:
print("User not found")
else:
print("Found:", user)
Expected output:
User not found
dict.get() returns None when the key does not exist, so find_user() returns None for an unknown ID.
None as a Default Parameter Value
None is commonly used as the default value of an optional parameter:
def greet(name=None):
if name is None:
print("Hello, guest!")
else:
print(f"Hello, {name}!")
greet()
greet("Meena")
Expected output:
Hello, guest!
Hello, Meena!
This pattern is also the standard fix for mutable default arguments, such as def add_item(item, items=None):, as explained in the Python Functions lesson.
Why None Is Useful
- Indicates missing or unavailable data — such as an optional field in a form
- Acts as a default placeholder — a variable can exist before its value is known
- Signals "no result" — functions can return
Nonewhen nothing is found - Makes code logic clearer — an explicit "no value" is easier to understand than a magic value like
-1
Common Mistakes
| Mistake | Problem |
|---|---|
Writing none or NULL | NameError — the keyword is None |
Using == None | Works in most cases, but is None is the correct style |
Using if not value: when 0 or "" is valid | Treats valid values as missing |
Calling a method on None | AttributeError: 'NoneType' object has no attribute ... |
Assigning the result of sort() or append() | The variable becomes None |
The AttributeError above is one of the most common errors in Python. It usually means a function returned None when you expected a real value:
result = None
result.upper()
Output:
AttributeError: 'NoneType' object has no attribute 'upper'
Related Concepts
- Python Booleans — truthy and falsy values
- Python Operators — identity operators
isandis not - Python Functions — return values and default parameters
- Python Dictionaries —
get()returningNonefor missing keys