Methods with return values let you get a result back after doing some work. This helps you use that result later in your program.
Methods with return values in Python
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Python
def method_name(parameters): # do some work return value
The return keyword sends the result back to where the method was called.
Once return runs, the method stops running any further code inside it.
Examples
Python
def add(a, b): return a + b
Python
def is_even(num): return num % 2 == 0
Python
def greet(name): return f"Hello, {name}!"
Sample Program
This program defines a method that multiplies two numbers and returns the product. Then it calls the method and prints the result.
Python
def multiply(x, y): return x * y result = multiply(4, 5) print(f"4 times 5 is {result}")
Important Notes
You can return any type of value: numbers, text, lists, or even other methods.
If a method does not have a return statement, it returns None by default.
Use return values to keep your code clean and easy to understand.
Summary
Methods with return values give back a result to use later.
Use return to send the result out of the method.
Returned values help make your code reusable and organized.
Practice
1. What does a method with a
return statement do in Python?easy
Solution
Step 1: Understand the purpose of
Thereturnreturnstatement 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 AQuick 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
Solution
Step 1: Identify correct return usage
The method must usereturnto send back the suma + b.Step 2: Check each option
The version withreturn a - breturns the difference. The version withprint(a + b)prints but returnsNone. The version with justa + blacksreturn. Onlydef add(a, b): return a + bcorrectly returns the sum.Final Answer:
def add(a, b): return a + b -> Option CQuick 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
Solution
Step 1: Understand the method call
The methodmultiplyreturns the product of 3 and 4, which is 12.Step 2: Print the returned value
The variableresultstores 12, soprint(result)outputs 12.Final Answer:
12 -> Option BQuick 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:
def greet(name):
print("Hello, " + name)
message = greet("Alice")
print(message)medium
Solution
Step 1: Identify the problem with return value
The method prints but does not return a value, somessageis None.Step 2: Fix by returning the greeting string
Replaceprintwithreturnto send the greeting back.Final Answer:
Changeprinttoreturninside the method. -> Option DQuick 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
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 buildingresult = {}, settingresult[i] = i * iforiinrange(1, n+1), and returningresultis correct. Returning a list fails. The comprehension{i: i + i for i in range(1, n)}uses doubles instead of squares and misses keyn. Printing without returning fails.Final Answer:
def squares(n): result = {} for i in range(1, n+1): result[i] = i * i return result -> Option AQuick 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]
Common Mistakes:
- Returning list instead of dict
- Using wrong range limits
- Printing instead of returning
