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

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands 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 your subtitle preferences 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 refer to our video player troubleshooting guide for assistance.

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.

wb7ond on Feb. 10, 2022

I get a run error for the st.mode function. Seems, from my duck duck search, I found that if you have more than one identical multiple occurrence of the same number, it errors as there are two results. The sample in the video had a pair of 9 and a pair of 7. If I add one more 9 or 7, OR delete a 7 or 9, the error stops… Or am I missing something???

R. Linder

wb7ond on Feb. 10, 2022

I see now, that being 1/10th (3.7.9) version shy of 3.8 brought this on. Seems that 3.9, it works ok… Disregard..

R. Linder

Become a Member to join the conversation.