Python None
This article is a complementary resource to the Learn Python Basics course.
This article is a complementary resource to the Learn Python Basics course.
The
None
keyword in Python represents the absence of a value or a null value.
Let's explore some common scenarios where
None
is used in Python and see how it helps in different situations.
When a function doesn't explicitly return a value, it implicitly returns
None
. Let's look at an example:
def greet(name):
print("Hello,", name)
result = greet("Alice")
print(result)
Output
Hello, Alice None
Here,
greet()
does not have a
return
statement, so it implicitly returns
None
. Hence, when we print the
result
of calling
greet()
, we see
None
as the output.
You can assign
None
to a variable to signify that it currently holds no value. This is useful when initializing variables that will be updated later. For example,
user_input = None
if user_input is None:
print("No input provided yet.")
else:
print("User input:", user_input)
# Output: No input provided yet.
In this example, the
user_input
variable initialized with
None
indicates that it has no value yet.
The condition
if user_input is None
checks whether
user_input
is still unset. Since
user_input
is indeed
None
, the program executes the first branch of the
if...else
statement
None
is a unique Python constant used to represent the
absence of a value.return
statement, it implicitly returns
None
.None
to a variable to signify
no value yet.Using
None
effectively in your code can help you manage variables, handle missing data, and design clear, intuitive functions.