0
0
Pythonprogramming~20 mins

Multiple return values in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Multiple Return Values Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
What is the output of this function returning multiple values?
Consider this Python function that returns multiple values. What will be printed when calling result = func() and then print(result)?
Python
def func():
    return 1, 2, 3

result = func()
print(result)
A(1, 2, 3)
B[1, 2, 3]
C1 2 3
DError: cannot return multiple values
Attempts:
2 left
๐Ÿ’ก Hint
Remember that returning multiple values in Python actually returns a tuple.
โ“ Predict Output
intermediate
2:00remaining
What is the value of variables after unpacking multiple return values?
Given this code, what are the values of a, b, and c after execution?
Python
def get_values():
    return 10, 20, 30

a, b, c = get_values()
AError: too many values to unpack
Ba=10, b=20, c=(30,)
Ca=(10, 20), b=30, c=None
Da=10, b=20, c=30
Attempts:
2 left
๐Ÿ’ก Hint
Check how many values the function returns and how many variables are on the left side.
โ“ Predict Output
advanced
2:00remaining
What is the output when returning multiple values with a conditional expression?
What will this code print?
Python
def check(num):
    return (num, 'even') if num % 2 == 0 else (num, 'odd')

print(check(7))
A(7, 'odd')
B(7, 'even')
CError: invalid syntax
D7 odd
Attempts:
2 left
๐Ÿ’ก Hint
Look at the condition and what tuple is returned for odd numbers.
โ“ Predict Output
advanced
2:00remaining
What error occurs when unpacking fewer variables than returned?
What happens when you run this code?
Python
def data():
    return 1, 2, 3

x, y = data()
Ax=(1, 2, 3), y=None
Bx=1, y=2
CValueError: too many values to unpack (expected 2)
DSyntaxError
Attempts:
2 left
๐Ÿ’ก Hint
Check how many values are returned vs how many variables are on the left.
๐Ÿง  Conceptual
expert
2:00remaining
How does Python handle multiple return values internally?
When a Python function returns multiple values separated by commas, what is actually returned?
AMultiple separate return statements executed sequentially
BA tuple containing all the values
CA list containing all the values
DA dictionary with keys as indices and values as returned values
Attempts:
2 left
๐Ÿ’ก Hint
Think about what data structure groups multiple values in Python without keys.