Course Content
Python Functions Tutorial
Python Functions Tutorial
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:
Using a List or Tuple
We can create a list or tuple that contains all necessary objects inside the function and return it as a result of the function.
# 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)
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.
Using Multiple Return Values
You can directly return multiple values separated by commas. When function is called, the results are captured in separate variables.
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)
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.
Swipe to show code editor
-
Create a function that calculates the following data statistics:
sum(total)
,average
,minimum
andmaximum
. -
Return all values, storing them in separate variables when calling the function.
Thanks for your feedback!
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:
Using a List or Tuple
We can create a list or tuple that contains all necessary objects inside the function and return it as a result of the function.
# 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)
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.
Using Multiple Return Values
You can directly return multiple values separated by commas. When function is called, the results are captured in separate variables.
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)
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.
Swipe to show code editor
-
Create a function that calculates the following data statistics:
sum(total)
,average
,minimum
andmaximum
. -
Return all values, storing them in separate variables when calling the function.
Thanks for your feedback!
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:
Using a List or Tuple
We can create a list or tuple that contains all necessary objects inside the function and return it as a result of the function.
# 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)
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.
Using Multiple Return Values
You can directly return multiple values separated by commas. When function is called, the results are captured in separate variables.
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)
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.
Swipe to show code editor
-
Create a function that calculates the following data statistics:
sum(total)
,average
,minimum
andmaximum
. -
Return all values, storing them in separate variables when calling the function.
Thanks for your feedback!
Sometimes it's necessary to return multiple objects as a result of the function. We can do it using two different approaches:
Using a List or Tuple
We can create a list or tuple that contains all necessary objects inside the function and return it as a result of the function.
# 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)
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.
Using Multiple Return Values
You can directly return multiple values separated by commas. When function is called, the results are captured in separate variables.
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)
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.
Swipe to show code editor
-
Create a function that calculates the following data statistics:
sum(total)
,average
,minimum
andmaximum
. -
Return all values, storing them in separate variables when calling the function.