You can return nothing in Python from a function in three ways. The first way is by omitting a return statement.
def someFunction(x):
x = x * 2
print(someFunction(2))
#Output:
None
The second way is by including a blank return statement.
def someFunction(x):
x = x * 2
return
print(someFunction(2))
#Output:
None
The third way is by returning None explicitly.
def someFunction(x):
x = x * 2
return None
print(someFunction(2))
#Output:
None
When working with functions in Python, sometimes you don’t want a function to return anything.
In this case, you might be asking, how do I return nothing from a function in Python?
Well, to return nothing, you have three options.
The first way is by omitting a return statement. In this case, you are not returning anything and this returns None.
def someFunction(x):
x = x * 2
print(someFunction(2))
#Output:
None
The second way is by including a blank return statement. A blank return statement implicitly returns None.
def someFunction(x):
x = x * 2
return
print(someFunction(2))
#Output:
None
The third way is by returning None explicitly. By returning None, you can see explicitly that you are getting back None when you call the function.
def someFunction(x):
x = x * 2
return None
print(someFunction(2))
#Output:
None
Pick any of these three options and you will succeed with returning nothing from a function in Python.
Hopefully this article has been useful for you to learn how to return nothing in Python.