What if your functions could hand you the answers to use anywhere, anytime?
Why Methods with return values in Python? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you want to calculate the total price of items in a shopping cart. You write code that prints the total directly inside a function, but then you realize you need that total for other tasks too, like applying discounts or taxes.
Without return values, you must repeat calculations or rely on printing results, which makes your code messy and hard to reuse. You can't easily pass results between parts of your program, leading to mistakes and extra work.
Methods with return values let you send back results from a function to where it was called. This way, you can save, reuse, or change the returned data anywhere in your program, making your code cleaner and more flexible.
def total_price(items): total = sum(items) print(total) # Can't use total elsewhere
def total_price(items): return sum(items) total = total_price([10, 20, 30]) print(total) # Now you can reuse total
You can build programs that share and reuse results easily, making your code smarter and more powerful.
Think of a calculator app: each button press calls a method that returns a number, which the app then uses to update the display or perform further calculations.
Return values let methods send results back to the caller.
This avoids repeating work and keeps code organized.
It makes your programs flexible and easier to maintain.
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
