Python None and NoneType
Understand Python's None value, what NoneType means, how functions produce None, and how to test for the absence of a value.
Basic
Python uses
None for a value that represents the absence of a value in many situations.
None is not the same thing as zero, an empty string, or
False.
There is one
None object. The built-in name points to that object, and its type is
NoneType.
None is its own value and type
None is the sole instance of
NoneType. You normally use the name
None directly rather than constructing a value of this type.
Output
Ready to run.
Functions can produce None
A function that reaches the end without a
return value returns
None. An explicit
return None makes the same result visible in the source code.
Output
Ready to run.
None is different from an empty value
An empty string, empty list, and zero are real values with their own types.
None instead communicates that there is no value in the role where the value would normally appear.
Output
Ready to run.
Check for None with identity
When the question is specifically whether a value is
None, write
value is None or
value is not None. This expresses the identity test directly.
Output
Ready to run.
None is false in a Boolean context
The truth-value of
None is false. That does not mean every false value is
None. Use an identity test when you need to distinguish missing data from other false values.
Output
Ready to run.
None often marks an absent result
A program can use
None to mean that a result, setting, or piece of information is not available. The surrounding program decides what that absence means;
None itself does not describe why the value is absent.
Output
Ready to run.
Key idea
Use
None when you need one clear value to represent absence, and use
is None when the distinction matters.
Thanks for your feedback.
Mark this learning resource complete to track your learning progress.