For more information on topics covered in this lesson, check out these resources:
Using Explicit Returns in Functions
Now let’s look at explicit
return statements. When a Python function executes an explicit
return statement, the function immediately ends its execution and sends the return value back to the calling environment. As previously mentioned, a
return statement begins with the keyword
return, followed by an optional return value. For example, in this simple function, the statement
return 42 means the number
42 will be returned back to where the function was called.
Just a simple function with a single
00:41 and that’s it! When I call this function,
I get the number
42 back. And since we are in the interpreter, that number is displayed. The return value for a function can be used in any expression, so I can save it to a variable or just use it directly from the function call.
Here I am saving it to the value
num, and there it is. But I can also do it with a calculation.
There I’m multiplying it by
And here I am adding
5 to it. And in each case, the number
42 is put in place of where the function call was in that expression, and then the rest of the expression is evaluated.
So I got
84, and then
47. The operations have to make sense.
Since this function returns a number, its function call can only be used where a number would be valid. You can’t use a
return statement outside of a function.
It generates a
SyntaxError. So if I try just a
return 42, there you can see at the bottom of the screen, my interpreter’s told me Syntax Error: ‘return’ outside function.
return statements only makes sense inside the body of a function. And recall that methods—regular methods, class methods, static methods—are just functions within the context of a class or an object, so anything you’ve seen here about functions applies to methods as well.
02:41 Here’s another example. This function expects a list of integers as a parameter and returns a new list with only the even numbers from the original list.
This function uses a Python syntax called list comprehension. It’s a way to simplify a common
if nested coding structure, which filters a list, looking for those elements meeting a specific condition.
Real Python has many resources for list comprehension, some of which are linked below. If we take a look at the function more closely, we see that it takes an argument, a list of numbers called
We then look through the list one element at a time for those numbers which are divisible by
2. If a number is divisible by
2, then its result modulus
0 itself is considered a
False value, so if you want the condition after the word
if to be
True, you have to put
not in front of it.
This will build up a new list, selecting from
numbers those elements which have a remainder of
0 when divided by
That list is saved to the variable
even_nums, which in the next line is used as the return value, so the return value here will be that newly created list.
and then call
get_even() on a list of numbers,
since we’re in the interpreter, the return value will be displayed when the function finishes executing. We can see we get the numbers
6, that list created from the original list.
The return value can be any valid Python expression. We could simplify the
get_even() method by just returning the result from the list comprehension without saving it to a different variable name.
05:07 So I could remove that, and just return the list comprehension. Let’s save that,
05:22 and I’ll restart my environment so I can re-import it.
from evens import our new
get_even. But again, give it a list of some numbers and it will provide for us a new list with just the even numbers.
05:48 Here’s another example where I can use Python’s sum and length functions to write a new function to find the mean of a collection of numbers. Just returning the expression, we’re going to take the sum divided by the length.
06:03 I won’t work through this one, but feel free to experiment with it on your own.
06:09 A common source of confusion is the difference between returning a value and outputting a value, and we’ll go into that next.
Hi @j63811, there’s no newbie rubbishing going on here, and all questions are valid!
My thoughts on your observation is that it really depends on what are your preferences. There are no hard rules here, only soft ideas of what (maybe) a majority (?) of the Python community considers as “idiomatic” or “pythonic”. You might have heard these terms somewhere.
Some would argue that the falsy nature of the value
0 gives you the opportunity to shorten the statement by evaluating a non-boolean value in a boolean context. This approach and code length reduction might make it more readable for them:
if not number % 2: # Your code
You might argue that this obfuscates the actual value of the modulo operation, where the result isn’t
False, but the number
0. Therefore, you prefer to be explicit:
if number % 2 == 0: # Your code
Both of these approaches are perfectly fine, and you should use the one that makes more sense to you. :)
There are always many ways to do the same thing in programming. It’s good to be clear and concise with your code, and make sure that you’ll still be able to understand what your intention were also months in the future. Hope that helps!
Thank you Martin great, when learning it’s difficult to tell if a trait is symbolic or traditional or just too clever for it’s own good. Just to be nudged into knowing that you are understanding but have preferences is very helpful
Become a Member to join the conversation.
j63811 on Aug. 29, 2021
Good tutorial and thank you, I am going to be newbie rubbished and sorry in advance but I just dont see how ‘If Not number % 2” is more explicit or immediate than ” If number % 2 == 0 ” what am I missing ?