Bird
Raised Fist0
Pythonprogramming~20 mins

Multiple return values in Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1.

What does a Python function return when it has multiple values separated by commas in the return statement?

easy
A. A tuple containing all the returned values
B. A list containing all the returned values
C. Only the first value is returned
D. A dictionary with values as keys

Solution

  1. Step 1: Understand Python return behavior

    When multiple values are separated by commas in a return statement, Python groups them into a tuple automatically.
  2. Step 2: Identify the returned data type

    The returned object is a tuple containing all the values, not a list or dictionary.
  3. Final Answer:

    A tuple containing all the returned values -> Option A
  4. Quick Check:

    Multiple values returned = tuple [OK]
Hint: Multiple returns become a tuple automatically [OK]
Common Mistakes:
  • Thinking it returns a list
  • Assuming only one value is returned
  • Confusing tuple with dictionary
2.

Which of the following is the correct syntax to return multiple values a and b from a function?

easy
A. return a, b
B. return {a: b}
C. return [a, b]
D. return {a, b}

Solution

  1. Step 1: Review return syntax for multiple values

    Python allows returning multiple values separated by commas without parentheses.
  2. Step 2: Compare options

    return a, b is correct and returns a tuple. return {a: b} returns a dictionary, return [a, b] returns a list, and return {a, b} returns a set, which are not typical for multiple return values.
  3. Final Answer:

    return a, b -> Option A
  4. Quick Check:

    Comma-separated values return tuple [OK]
Hint: Use commas without brackets to return multiple values [OK]
Common Mistakes:
  • Using brackets unnecessarily
  • Returning list or set instead of tuple
  • Syntax errors with braces
3.

What will be the output of this code?

def get_values():
    return 5, 10

x, y = get_values()
print(x + y)
medium
A. 510
B. TypeError
C. 15
D. None

Solution

  1. Step 1: Understand function return and unpacking

    The function returns two values 5 and 10 as a tuple. Variables x and y capture these values separately.
  2. Step 2: Calculate the sum

    Adding x and y means 5 + 10 which equals 15.
  3. Final Answer:

    15 -> Option C
  4. Quick Check:

    5 + 10 = 15 [OK]
Hint: Unpack returned tuple into variables to use values [OK]
Common Mistakes:
  • Concatenating numbers as strings
  • Forgetting to unpack tuple
  • Expecting a list instead of tuple
4.

Find the error in this code snippet:

def split_name(full_name):
    first, last = full_name.split()
    return first, last

f, l, m = split_name('John Doe')
medium
A. Missing return statement
B. Too many variables to unpack returned values
C. Function split_name returns a list, not tuple
D. split() method does not work on strings

Solution

  1. Step 1: Analyze returned values and unpacking

    The function returns two values: first and last name. The caller tries to unpack into three variables.
  2. Step 2: Identify mismatch in unpacking

    Trying to assign two returned values to three variables causes a ValueError due to too many variables to unpack.
  3. Final Answer:

    Too many variables to unpack returned values -> Option B
  4. Quick Check:

    Returned 2 values but unpacked into 3 variables [OK]
Hint: Match number of variables to returned values [OK]
Common Mistakes:
  • Unpacking into more variables than returned
  • Assuming split returns tuple always
  • Ignoring ValueError messages
5.

Given this function that returns multiple values, how can you modify it to return the sum and product of a list of numbers?

def calculate(numbers):
    # Your code here

result = calculate([1, 2, 3, 4])
print(result)

Choose the correct implementation to return both sum and product.

hard
A. def calculate(numbers): total = sum(numbers) product = 0 for n in numbers: product += n return total, product
B. def calculate(numbers): return sum(numbers) + product(numbers)
C. def calculate(numbers): return [sum(numbers), product(numbers)]
D. def calculate(numbers): total = sum(numbers) product = 1 for n in numbers: product *= n return total, product

Solution

  1. Step 1: Calculate sum and product correctly

    Sum is calculated using built-in sum(). Product requires initializing to 1 and multiplying each number.
  2. Step 2: Return both values as multiple return values

    Return total and product separated by comma to return a tuple.
  3. Final Answer:

    def calculate(numbers): total = sum(numbers) product = 1 for n in numbers: product *= n return total, product -> Option D
  4. Quick Check:

    Return sum and product as tuple [OK]
Hint: Return sum and product separated by comma [OK]
Common Mistakes:
  • Using product = 0 and adding instead of multiplying
  • Trying to add sum and product instead of returning both
  • Returning list instead of tuple