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
course content

Conteúdo do Curso

Python Functions Tutorial

Python Functions Tutorial

1. What is Function in Python?
2. Positional and Optional Arguments
3. Arbitrary Arguments
4. Function Return Value Specification
5. Recursion and Lambda Functions

Multiple 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.

123456789101112
# Define a function def return_multiple_objects(): obj1 = 'Hello' obj2 = 42 obj3 = [1, 2, 3] # Return all objects packed into list return [obj1, obj2, obj3] # Get the list with corresponding objects result_list = return_multiple_objects() for obj in result_list: print(obj)
copy

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.

12345678910
def return_multiple_objects(): obj1 = "Hello" obj2 = 42 obj3 = [1, 2, 3] # Return objects separated by comma return obj1, obj2, obj3 # Get the result of the function into three different values result1, result2, result3 = return_multiple_objects() print(result1, result2, result3)
copy

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.

Tarefa

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.

Tarefa

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.

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo

Tudo estava claro?

Seção 4. Capítulo 2
toggle bottom row

Multiple 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.

123456789101112
# Define a function def return_multiple_objects(): obj1 = 'Hello' obj2 = 42 obj3 = [1, 2, 3] # Return all objects packed into list return [obj1, obj2, obj3] # Get the list with corresponding objects result_list = return_multiple_objects() for obj in result_list: print(obj)
copy

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.

12345678910
def return_multiple_objects(): obj1 = "Hello" obj2 = 42 obj3 = [1, 2, 3] # Return objects separated by comma return obj1, obj2, obj3 # Get the result of the function into three different values result1, result2, result3 = return_multiple_objects() print(result1, result2, result3)
copy

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.

Tarefa

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.

Tarefa

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.

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo

Tudo estava claro?

Seção 4. Capítulo 2
toggle bottom row

Multiple 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.

123456789101112
# Define a function def return_multiple_objects(): obj1 = 'Hello' obj2 = 42 obj3 = [1, 2, 3] # Return all objects packed into list return [obj1, obj2, obj3] # Get the list with corresponding objects result_list = return_multiple_objects() for obj in result_list: print(obj)
copy

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.

12345678910
def return_multiple_objects(): obj1 = "Hello" obj2 = 42 obj3 = [1, 2, 3] # Return objects separated by comma return obj1, obj2, obj3 # Get the result of the function into three different values result1, result2, result3 = return_multiple_objects() print(result1, result2, result3)
copy

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.

Tarefa

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.

Tarefa

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.

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo

Tudo estava claro?

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.

123456789101112
# Define a function def return_multiple_objects(): obj1 = 'Hello' obj2 = 42 obj3 = [1, 2, 3] # Return all objects packed into list return [obj1, obj2, obj3] # Get the list with corresponding objects result_list = return_multiple_objects() for obj in result_list: print(obj)
copy

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.

12345678910
def return_multiple_objects(): obj1 = "Hello" obj2 = 42 obj3 = [1, 2, 3] # Return objects separated by comma return obj1, obj2, obj3 # Get the result of the function into three different values result1, result2, result3 = return_multiple_objects() print(result1, result2, result3)
copy

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.

Tarefa

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.

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Seção 4. Capítulo 2
Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
We're sorry to hear that something went wrong. What happened?
some-alt