Bird
Raised Fist0
Pythonprogramming~20 mins

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
๐ŸŽ–๏ธ
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 call?
Consider the following Python function. What will be printed when result = multiply_and_add(2, 3) is executed and then print(result) is called?
Python
def multiply_and_add(x, y):
    product = x * y
    sum_ = x + y
    return product + sum_

result = multiply_and_add(2, 3)
print(result)
A11
B10
C5
D6
Attempts:
2 left
๐Ÿ’ก Hint
Remember to add both the product and the sum of the two numbers.
โ“ Predict Output
intermediate
2:00remaining
What does this function return?
What is the output of the following code snippet?
Python
def check_even(num):
    if num % 2 == 0:
        return True
    else:
        return False

print(check_even(7))
AFalse
BTrue
CNone
D7
Attempts:
2 left
๐Ÿ’ก Hint
Check if 7 is divisible by 2 without remainder.
โ“ Predict Output
advanced
2:00remaining
What is the output of this recursive function?
What will be printed when the following code runs?
Python
def factorial(n):
    if n == 0:
        return 1
    return n * factorial(n - 1)

print(factorial(4))
A0
B24
C10
D120
Attempts:
2 left
๐Ÿ’ก Hint
Factorial of 4 is 4 * 3 * 2 * 1.
โ“ Predict Output
advanced
2:00remaining
What is the value of x after this function call?
What is the value of x after running the code below?
Python
def add_to_list(lst, item):
    lst.append(item)
    return lst

my_list = [1, 2]
x = add_to_list(my_list, 3)
print(x)
ANone
B[1, 2]
C[3]
D[1, 2, 3]
Attempts:
2 left
๐Ÿ’ก Hint
The function appends an item to the list and returns it.
โ“ Predict Output
expert
2:00remaining
What is the output of this function with multiple return statements?
What will be printed when the following code runs?
Python
def test_return(x):
    if x > 0:
        return 'Positive'
    elif x == 0:
        return 'Zero'
    return 'Negative'

print(test_return(-5))
ANone
BZero
CNegative
DPositive
Attempts:
2 left
๐Ÿ’ก Hint
Check the conditions for x = -5.

Practice

(1/5)
1. What does the return statement do in a Python function?
easy
A. It sends a value back to where the function was called.
B. It prints a value to the screen.
C. It creates a new variable inside the function.
D. It pauses the function without sending any value.

Solution

  1. Step 1: Understand the purpose of return

    The return statement ends the function and sends a value back to the caller.
  2. Step 2: Differentiate from printing or pausing

    Printing shows output but does not send a value back; pausing does not return a value.
  3. Final Answer:

    It sends a value back to where the function was called. -> Option A
  4. Quick Check:

    Return sends value back [OK]
Hint: Return sends value back, print shows output only [OK]
Common Mistakes:
  • Confusing return with print
  • Thinking return pauses without value
  • Assuming return creates variables
2. Which of the following is the correct syntax to return the value 10 from a function?
easy
A. return(10,)
B. return 10
C. return = 10
D. return: 10

Solution

  1. Step 1: Recall correct return syntax

    The correct syntax is simply return followed by the value without assignment or colon.
  2. Step 2: Check each option

    return 10 is correct; return(10,) returns a tuple (10,) due to the trailing comma; the others cause syntax errors.
  3. Final Answer:

    return 10 -> Option B
  4. Quick Check:

    Return value with 'return value' syntax [OK]
Hint: Use 'return value' without assignment or colon [OK]
Common Mistakes:
  • Using assignment with return
  • Adding colon after return
  • Using parentheses incorrectly
3. What is the output of this code?
def add(a, b):
    return a + b

result = add(3, 4)
print(result)
medium
A. None
B. 34
C. 7
D. Error

Solution

  1. Step 1: Understand the function behavior

    The function add returns the sum of a and b.
  2. Step 2: Calculate the return value

    Calling add(3, 4) returns 7, which is stored in result and printed.
  3. Final Answer:

    7 -> Option C
  4. Quick Check:

    3 + 4 = 7 [OK]
Hint: Return value is sum, printed as 7 [OK]
Common Mistakes:
  • Concatenating numbers as strings
  • Forgetting to return value
  • Expecting print inside function
4. Find the error in this function and choose the correct fix:
def multiply(x, y):
    product = x * y
    print(product)
medium
A. Add return x, y instead of product.
B. Change print(product) to return print(product).
C. Remove product and just print x * y.
D. Add return product to send the result back.

Solution

  1. Step 1: Identify the problem

    The function prints the product but does not return it, so no value is sent back.
  2. Step 2: Fix by returning the product

    Adding return product sends the result back to the caller.
  3. Final Answer:

    Add return product to send the result back. -> Option D
  4. Quick Check:

    Return value to send result back [OK]
Hint: Return value to send it back, print only shows output [OK]
Common Mistakes:
  • Returning print() instead of value
  • Not returning any value
  • Returning wrong variables
5. You want a function that returns the first non-empty string from a list or None if all are empty. Which function correctly does this?
hard
A. def first_non_empty(strings): for s in strings: if s: return s return None
B. def first_non_empty(strings): for s in strings: if s == '': return s return None
C. def first_non_empty(strings): for s in strings: if not s: return s return None
D. def first_non_empty(strings): for s in strings: return s return None

Solution

  1. Step 1: Understand the goal

    The function should return the first string that is not empty (truthy) or None if none found.
  2. Step 2: Analyze each option

    The version with if not s: return s returns the first falsy (empty) string; if s == '' returns the first exactly empty string; if s: return s returns the first truthy (non-empty) string; the version without if returns the first string regardless. Only the truthy check is correct.
  3. Final Answer:

    def first_non_empty(strings): for s in strings: if s: return s return None -> Option A
  4. Quick Check:

    Return first truthy string or None [OK]
Hint: Return first truthy value, else None [OK]
Common Mistakes:
  • Returning empty strings instead of non-empty
  • Returning on first loop without condition
  • Confusing truthy and falsy values