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
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.
def add(a, b): return a + b
def is_even(num): return num % 2 == 0
def greet(name): return f"Hello, {name}!"
This program defines a method that multiplies two numbers and returns the product. Then it calls the method and prints the result.
def multiply(x, y): return x * y result = multiply(4, 5) print(f"4 times 5 is {result}")
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.
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
return statement do in Python?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]
- Confusing return with print
- Thinking return stops the program
- Believing return creates variables
a and b?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]
- Using print instead of return
- Omitting return keyword
- Returning wrong expression
def multiply(x, y):
return x * y
result = multiply(3, 4)
print(result)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]
- Adding instead of multiplying
- Printing None by missing return
- Confusing string concatenation with multiplication
def greet(name):
print("Hello, " + name)
message = greet("Alice")
print(message)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]
- Expecting print to return a value
- Removing needed parameters
- Changing variable names incorrectly
n and values as their squares. Which method below correctly does this?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]
- Returning list instead of dict
- Using wrong range limits
- Printing instead of returning
Ready to prove it?
Pass the test and this pattern moves unknown
