Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Multiple Return Values | Function Return Value Specification
Python Functions Tutorial

Multiple Return ValuesMultiple Return Values

Sometimes it's necessary to return multiple objects as a result of the function. We can do it using two different approaches:

First approach

We can create a list or tuple that contains all necessary objects inside the function and return it as a result of the function.

We created three different objects inside the function and returned the list that contains all these objects as an output of the function. Then we iterated this list to get all objects.

Second approach

You can directly return multiple values separated by commas. When function is called, the results are captured in separate variables.

In the code above we returned three variables separately. If we use this approach it's very important to know the order in which the variables are returned to correctly use them in code.

Tarea

Create a function that calculates the following data statistics: sum(total), average, minimum and maximum. Return all these statistics using second approach in the order, described above. Call the function, get calculated statistics and print them.

¿Todo estuvo claro?

Sección 4. Capítulo 2
toggle bottom row
course content

Contenido del Curso

Python Functions Tutorial

Multiple Return ValuesMultiple Return Values

Sometimes it's necessary to return multiple objects as a result of the function. We can do it using two different approaches:

First approach

We can create a list or tuple that contains all necessary objects inside the function and return it as a result of the function.

We created three different objects inside the function and returned the list that contains all these objects as an output of the function. Then we iterated this list to get all objects.

Second approach

You can directly return multiple values separated by commas. When function is called, the results are captured in separate variables.

In the code above we returned three variables separately. If we use this approach it's very important to know the order in which the variables are returned to correctly use them in code.

Tarea

Create a function that calculates the following data statistics: sum(total), average, minimum and maximum. Return all these statistics using second approach in the order, described above. Call the function, get calculated statistics and print them.

¿Todo estuvo claro?

Sección 4. Capítulo 2
toggle bottom row
some-alt