Return Multiple Values From a Function
00:00 As the last item on the agenda for tuples, I want to show you a place where you may have used tuples implicitly without knowing about it. And that is how you can return multiple values from a function, which isn’t quite the right way to say this, but I’ll show you why in just a second.
In Python, you can define a function. Let’s say we’re going to call this one
get_stats(), and it’ll take as an argument, a sequence, and then it will return the maximum of that sequence and the minimum of that sequence.
01:00 But after going through this course, you’ll know that what you’re actually doing here is creating a tuple literal. So you could wrap this inside of parentheses as well, and it would be the same thing.
01:09 So you’re creating a tuple literal, and then you’re returning the tuple literal with these two elements in it. One of the elements is going to be the maximum of the sequence, and the other element is going to be the minimum of the sequence.
However, in a
return statement of a Python function, you very often are not going to see people putting the tuple parentheses, but instead they just write out the values they want to return with a comma.
01:34 This is a common way that you may see the definition of a function where the person wants to return multiple variables. What they really do is return a single tuple that contains the values that they want to return.
02:05 There’s also a tuple in that case. Could be a different sequence as well. If I press Enter, you can see that what Python returns is a tuple with first the maximum value and then the minimum value.
Let’s say you want to get the maximum and the minimum and work with those two values from some sort of a sequence. Then you can use this
get_stats() function to do that and combine it with tuple unpacking to get access to both of those values separately.
and then I’m going to call the
get_stats() function and pass it the sequence. In this case, this is our tuple
numbers. And now what happens here is that
numbers as an argument, figures out what’s the maximum and the minimum, then puts these two into a tuple literal and returns that tuple.
And then out here we’re unpacking the return value of this function into the two values that it returns. And you’re going to do tuple unpacking to assign the first value to the variable
maximum and the second value to the variable
So now you have access to
maximum, which points to
3 in this case, and
minimum which points to
1. That’s kind of interesting and a relatively common use case of, I would say, hidden tuples that you will find in Python because, like I mentioned before, it’s rare that someone actually returns the tuple in a more visible way.
03:55 By wrapping these two return values into additional parentheses, you can do it. It’s got the same effect. I would probably even suggest you to do it because I think it’s a little more obvious what’s happening and takes away this idea that it’s possible to return multiple values from a function.
04:26 So you can use tuples to return multiple values from a function. Really, you’re just returning the tuple, but the values are wrapped in there. And then you can use tuple unpacking to access those individual values from the tuple.
05:01 You’ll see that lists are used for a much wider variety of use cases than tuples. All right, in the next section, let’s start taking a look at lists. But before that, I have a few review exercises for you that you can try out to fortify the things that you’ve learned about in this section.
Become a Member to join the conversation.