Is return the same as return None?

Note: There is no such term as “return null” in Python. Every function returns some value (unless it raises an exception). If no explicit return statement is used, Python treats it as returning None

To literally return ‘nothing’ use pass, which basically returns the value None if put in a function(Functions must return a value, so why not ‘nothing’). You can do this explicitly and return None yourself though.

if x>1:
    return(x)
else:
    pass

OR

if x>1:
    return(x)
else:
    return None

Do comment if you have any doubts and suggestions on this Python return topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Is return the same as return None?

Rohit

Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.

If the function should return a value, the return statement in Python is no different than the languages I mentioned earlier:

Is return the same as return None?

But what if the function isn't supposed to return anything ? A great example are methods which act on the object at hand, such as my_list.sort(). That'll simply sort the list object calling it. 
And, what if the function isn't supposed to return anything for the actual input arguments ? 

Every function in Python which does not hit a return statement, will implicitly return None.
This leaves the Python developer with a few options:

- don't do anything (the function will implicitly return None)

- have an explicit return None statement

- have a bare return statement

Now, all of these options will return None, so there's no difference in behavior. It's purely a choice of coding style, but that doesn't mean it's irrelevant. A good style will make the code more expressive and readable.
Goes without saying that style is debatable at least to some extent, and there's no absolute right and wrong here. I'll just let you know how I personally go about it, and my short reasoning for it.

I go with the option of having a return None, when the function is indeed meant to return a value, but it can't do so on this particular conditional path. That could be due to invalid input, exceptions, or whatever business logic requirement.

I'll use no return statement whatsoever when the function simply isn't meant to return. That's just keeping things clean. Any return statement in this case adds on to code clutter.

I'll use a bare return when the function isn't meant to return, and I want to exit the function early. Sort of using it as a break statement. It's not something I do often though, and probably a remnant of my Java days.

Unfortunately, there is a character limit so this will be in many parts. First thing to note is that return and print are statements, not functions, but that is just semantics.

I’ll start with a basic explanation. print just shows the human user a string representing what is going on inside the computer. The computer cannot make use of that printing. return is how a function gives back a value. This value is often unseen by the human user, but it can be used by the computer in further functions.

On a more expansive note, print will not in any way affect a function. It is simply there for the human user’s benefit. It is very useful for understanding how a program works and can be used in debugging to check various values in a program without interrupting the program.

return is the main way that a function returns a value. All functions will return a value, and if there is no return statement (or yield but don’t worry about that yet), it will return None. The value that is returned by a function can then be further used as an argument passed to another function, stored as a variable, or just printed for the benefit of the human user.

The Python return keyword allows you to define the return value of a function (e.g., return X). If you don’t explicitly set a return value or you omit the return statement, Python will implicitly return the following default value: None.

Is return the same as return None?

There are three cases of what a function can return:

  • Case 1: The function has a return statement that returns an explicit value.
  • Case 2: The function has a return statement that returns explicitly nothing. It returns None.
  • Case 3: The function does not have a return statement. The function in that case implicitly returns None.

Let’s have an example of all three cases next!

Case 1: The function has a return statement that returns an explicit value.

def f1():
    return 42

print(f1())
# 42

Case 2: The function has a return statement that returns explicitly nothing. It returns None.

def f2():
    return

print(f2())
# None

Case 3: The function does not have a return statement. The function in that case implicitly returns None.

def f3():
    pass

print(f3())
# None

Feel free to check out our Python cheat sheets to learn about all the Python basics and keywords and tricks:

Is return the same as return None?

Chris

While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.

To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.

His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.

Is return same as return None Python?

Python Function without return statement Every function in Python returns something. If the function doesn't have any return statement, then it returns None .

What does it mean when a function returns None?

00:19 There are basically three ways to cause a value of None to be returned from a function: if the function doesn't have a return statement at all, if you have a return statement with no return value, or you can explicitly return None .

Can you return None in Python?

In Python, every function returns something. If there are no return statements, then it returns None. If the return statement contains an expression, it's evaluated first and then the value is returned.

What can I use instead of return?

Some common synonyms of return are reciprocate, requite, and retaliate. While all these words mean "to give back usually in kind or in quantity," return implies a paying or giving back.