Basics
None
None is Python's value for “nothing here.” Check it with is None because it is a singleton identity value.
Source
result = None
print(result is None)Output
TrueFunctions often return None when absence is expected and callers can continue. The function name and surrounding code should make that possibility clear.
Source
def find_score(name):
if name == "Ada":
return 10
return None
score = find_score("Grace")
print(score is None)Output
TrueA missing dictionary key is another absence boundary. Use get() when the mapping can supply a default, and use exceptions for invalid operations that cannot produce a value.
Source
profile = {"name": "Ada"}
print(profile.get("timezone", "UTC"))
try:
int("python")
except ValueError:
print("invalid number")Output
UTC
invalid numberNotes
- Use
is Nonerather than== None;Noneis a singleton identity value. - Use
Nonefor expected absence that callers can test. - Use dictionary defaults for missing mapping keys and exceptions for invalid operations.
See also
- related: Values
- related: Truthiness
- next depth: Exceptions
- next depth: Dictionaries
Run the complete example
Expected output
True
True
UTC
invalid number
Execution time appears here after you run the example.