Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Returning Multiple Values

00:00 Now we’ll look at how Python functions can return multiple values. In Python, a function can return multiple values using a single return statement by simply listing those values separated by commas.

00:14 I have to say that as a programmer coming from other languages into Python, this is an amazing feature. Here’s a function that uses three functions from the statistics module to compute and return three different measures of center in a single statement.

00:32 We provide the data to the parameter sample. Then, from the statistics module, we compute the mean, median, and mode, and return all of that in a single statement.

00:48 Python returns these values in a single tuple.

00:57 So, let’s create some sample data.

01:02 I’m using the same sample data that the article uses, so if you’re following along, we’ll see similar results. I create the sample data, call describe() on that data, saving the return value to a variable, and then display its value.

01:22 I get a tuple with the first value containing the mean, the second one containing the median, and the third one containing the mode because that’s the order they were in the return statement.

01:35 The values in the tuple can be saved to different variables using iterable unpacking. You use an assignment statement—in this case, with three variables on the left-hand side because there are three elements in the returned tuple—and each value in the tuple is saved to a different variable.

01:56 We can see that each variable has a single value. mean is the mean for the data, median was the computed median for that data, and mode contained the mode of that data.

02:14 Python has some built-in functions that also return multiple values. For example, consider the divmod() function. This function provides the quotient and remainder for integer division.

02:28 When a CPU performs integer division, even if you’re asking for just the quotient or just the modulus, it usually computes both. That’s just how the algorithm works, so you might as well have a function that returns both of those values as well.

02:44 We can see that 15 divided by 3 is 5 with a remainder of 0, and 8 divided by 3 is 2 with a remainder of 2.

02:57 We could’ve saved this tuple to a single variable. We could have used iterable unpacking to save them to multiple variables. In this case, the left-hand side of the assignment statement would have had just two variables: one to contain the quotient and the other to contain the modulus, the remainder.

03:18 Next, we’ll start lessons on best practices using the Python return statement.

Become a Member to join the conversation.