0
0
Pythonprogramming~10 mins

Multiple return values in Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Multiple return values
Function called
Execute function body
Prepare multiple values
Return values as tuple
Caller receives tuple
Unpack or use returned values
When a function returns multiple values, it actually returns them as a tuple. The caller can then unpack or use these values separately.
Execution Sample
Python
def get_stats(numbers):
    total = sum(numbers)
    count = len(numbers)
    average = total / count
    return total, count, average

result = get_stats([10, 20, 30])
This function calculates total, count, and average of a list and returns all three values together.
Execution Table
StepActionVariablesReturned ValueNotes
1Function get_stats called with [10, 20, 30]numbers=[10,20,30]Start function execution
2Calculate total = sum(numbers)total=60Sum of list elements
3Calculate count = len(numbers)count=3Number of elements
4Calculate average = total / countaverage=20.0Average value
5Return total, count, averagetotal=60, count=3, average=20.0(60, 3, 20.0)Multiple values returned as tuple
6Assign returned tuple to resultresult=(60, 3, 20.0)Caller receives tuple
💡 Function completes after returning the tuple of three values
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
numbers[10, 20, 30][10, 20, 30][10, 20, 30][10, 20, 30][10, 20, 30]
totalundefined60606060
countundefinedundefined333
averageundefinedundefinedundefined20.020.0
resultundefinedundefinedundefinedundefined(60, 3, 20.0)
Key Moments - 2 Insights
Why does the function return a single tuple instead of separate values?
The function returns multiple values separated by commas, which Python automatically packs into a tuple (see execution_table step 5). This tuple is a single object containing all values.
How can the caller use the multiple returned values?
The caller receives a tuple (execution_table step 6). They can unpack it into separate variables or use it as a tuple directly.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 5, what is the returned value?
A(60, 3, 20.0)
B60, 3, 20.0
C[60, 3, 20.0]
DNone
💡 Hint
Check the 'Returned Value' column at step 5 in execution_table
At which step is the variable 'average' first assigned a value?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look at the 'Variables' column in execution_table for when 'average' appears
If the function returned only 'total' instead of multiple values, how would the 'result' variable change?
Aresult would be a tuple with one element
Bresult would be a list
Cresult would be a single number, not a tuple
Dresult would be undefined
💡 Hint
Refer to how multiple values are packed into a tuple in execution_table step 5 and 6
Concept Snapshot
def func():
    return val1, val2, val3

# Returns multiple values as a tuple
# Caller can unpack: a, b, c = func()
# Or use as tuple: result = func()
Full Transcript
This example shows a Python function returning multiple values. The function get_stats calculates total, count, and average of a list. It returns these three values separated by commas. Python packs them into a tuple automatically. The caller receives this tuple and can unpack or use it. The execution table traces each step: calling the function, calculating variables, returning the tuple, and assigning it to result. Variables change as the function runs, and the final result is a tuple with three values. Beginners often wonder why multiple values are returned as one tuple and how to use them. The quiz questions help check understanding of these points.