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
Recall & Review
beginner
What does a method with a return value do?
It performs a task and then sends back a result to the place where it was called.
Click to reveal answer
beginner
How do you define a method that returns a value in Python?
Use the def keyword to define the method, then use return followed by the value you want to send back.
Click to reveal answer
beginner
What happens if a method does not have a return statement?
It returns None by default, which means it sends back no useful value.
Click to reveal answer
beginner
Why is it useful for a method to return a value?
Because it lets you use the result later in your program, like saving it in a variable or printing it.
Click to reveal answer
beginner
Example: What does this method return?
def add(a, b):
return a + b
It returns the sum of a and b. For example, add(2, 3) returns 5.
Click to reveal answer
What keyword is used to send a value back from a method in Python?
Aoutput
Bsend
Cyield
Dreturn
✗ Incorrect
The return keyword is used to send a value back from a method.
What does a method return if there is no return statement?
ANone
BAn error
CFalse
D0
✗ Incorrect
If no return statement is present, Python methods return None by default.
Which of these is a correct method that returns the square of a number?
Adef square(x): x * x
Bdef square(x): print(x * x)
Cdef square(x): return x * x
Ddef square(x): return
✗ Incorrect
Only option A returns the square value properly using return.
Why might you want a method to return a value?
ATo reuse the result later
BTo stop the program
CTo print something
DTo create a new method
✗ Incorrect
Returning a value lets you reuse the result later in your program.
What will this code print?
def greet():
return 'Hello!'
print(greet())
ANone
BHello!
Cgreet
DError
✗ Incorrect
The method returns 'Hello!' which is then printed.
Explain how a method with a return value works and why it is useful.
Think about how you get answers back when you ask a question.
You got /3 concepts.
Write a simple Python method that takes two numbers and returns their product.
Use <code>def</code> and <code>return a * b</code>.
You got /3 concepts.
Practice
(1/5)
1. What does a method with a return statement do in Python?
easy
A. It sends a value back to where the method was called.
B. It prints a value on the screen.
C. It stops the program immediately.
D. It creates a new variable automatically.
Solution
Step 1: Understand the purpose of return
The return statement sends a value back from the method to the caller.
Step 2: Differentiate from printing or stopping
Printing shows output but does not send a value back; stopping ends execution.
Final Answer:
It sends a value back to where the method was called. -> Option A
Quick Check:
Method return = sends value back [OK]
Hint: Return sends value back, print shows it only [OK]
Common Mistakes:
Confusing return with print
Thinking return stops the program
Believing return creates variables
2. Which of the following is the correct syntax for a method that returns the sum of two numbers a and b?
easy
A. def add(a, b): return a - b
B. def add(a, b): print(a + b)
C. def add(a, b): return a + b
D. def add(a, b): a + b
Solution
Step 1: Identify correct return usage
The method must use return to send back the sum a + b.
Step 2: Check each option
The version with return a - b returns the difference. The version with print(a + b) prints but returns None. The version with just a + b lacks return. Only def add(a, b): return a + b correctly returns the sum.
Final Answer:
def add(a, b): return a + b -> Option C
Quick Check:
Return sum = def add(a, b): return a + b [OK]
Hint: Return must be followed by value to send back [OK]
Common Mistakes:
Using print instead of return
Omitting return keyword
Returning wrong expression
3. What is the output of this code?
def multiply(x, y):
return x * y
result = multiply(3, 4)
print(result)
medium
A. 7
B. 12
C. 34
D. None
Solution
Step 1: Understand the method call
The method multiply returns the product of 3 and 4, which is 12.
Step 2: Print the returned value
The variable result stores 12, so print(result) outputs 12.
Final Answer:
12 -> Option B
Quick Check:
3 * 4 = 12 [OK]
Hint: Multiply inputs, return result, print shows it [OK]
Common Mistakes:
Adding instead of multiplying
Printing None by missing return
Confusing string concatenation with multiplication
4. Find the error in this method and choose the correct fix:
The method prints but does not return a value, so message is None.
Step 2: Fix by returning the greeting string
Replace print with return to send the greeting back.
Final Answer:
Change print to return inside the method. -> Option D
Quick Check:
Return greeting to assign message [OK]
Hint: Use return to get value, not print [OK]
Common Mistakes:
Expecting print to return a value
Removing needed parameters
Changing variable names incorrectly
5. You want to write a method that returns a dictionary with keys as numbers from 1 to n and values as their squares. Which method below correctly does this?
hard
A. def squares(n):
result = {}
for i in range(1, n+1):
result[i] = i * i
return result
B. def squares(n):
result = []
for i in range(n):
result.append(i * i)
return result
C. def squares(n):
return {i: i + i for i in range(1, n)}
D. def squares(n):
for i in range(1, n+1):
print(i * i)
Solution
Step 1: Understand the requirement
The method must return a dictionary with keys 1 to n and values as squares.
Step 2: Check each option
The loop building result = {}, setting result[i] = i * i for i in range(1, n+1), and returning result is correct. Returning a list fails. The comprehension {i: i + i for i in range(1, n)} uses doubles instead of squares and misses key n. Printing without returning fails.
Final Answer:
def squares(n):
result = {}
for i in range(1, n+1):
result[i] = i * i
return result -> Option A
Quick Check:
Return dict with squares = def squares(n):
result = {}
for i in range(1, n+1):
result[i] = i * i
return result [OK]
Hint: Return dict with keys and squares using loop [OK]