Mastering Return Values in Python: A Guide for Beginners
Learn how to use the return keyword in Python to capture function output, store results in variables, and chain logic for cleaner, more efficient code.

Previously in this course, we explored defining custom functions and passing data via function arguments and parameters. While those lessons showed you how to get data into a function, we mostly relied on print() to see the results.
In real-world backend development, you rarely want your functions to just "talk" to the console. You want them to perform a calculation or transform data and then send that result back to the main program to be used elsewhere. This is where return comes in.
Understanding the Return Keyword
When a function executes code, it usually performs a specific task. If that task produces data you need to use later—like a processed user ID or a calculated total—you must use the return keyword.
Think of return as a hand-off. When a function hits a return statement, two things happen immediately:
- The function stops: Any code written after the
returnline inside that function is ignored. - The value is sent back: The specified data is "passed back" to wherever the function was called.
If you don't use return, Python functions implicitly return None (the absence of a value), which is why previous functions may have seemed to "disappear" after they finished running.
Storing Function Output in Variables

The primary reason we return values is to capture them. Once a function sends data back, you can store that data in a variable, just like you would with a raw number or string.
Consider this example where we calculate a tax total:
PYTHONdef calculate_tax(amount): tax_rate = 0.08 total = amount * tax_rate return total # Capture the output in a variable tax_amount = calculate_tax(100) print(f"The calculated tax is: ${tax_amount:.2f}")
By assigning calculate_tax(100) to tax_amount, the value 8.0 is stored in memory. You can now use tax_amount in other calculations, save it to a file, or send it to an API.
Chaining Function Calls
Because a function call evaluates to its returned value, you can use the result of one function directly as an argument for another. This is called "chaining," and it’s a powerful way to write concise, readable code.
Imagine we have a pipeline that cleans a string and then formats it:
PYTHONdef clean_input(text): return text.strip().lower() def wrap_in_tags(text): return f"<div>{text}</div>" # Chaining the functions raw_data = " Hello World " formatted_output = wrap_in_tags(clean_input(raw_data)) print(formatted_output) # Output: <div>hello world</div>
In this example, clean_input(raw_data) runs first, returning "hello world". That result is passed immediately into wrap_in_tags().
Hands-on Exercise: Building the Processor
Let's advance our project. Open your script and create a function that takes a list of numbers and returns the sum of those numbers. Then, use that returned sum to calculate an average.
- Define
get_sum(numbers_list). - Inside, use the built-in
sum()function to return the total. - Call the function, store the result in a variable called
total_sum. - Divide
total_sumby the length of the list and print the result.
Common Pitfalls
- Forgetting to capture the return: A common mistake is calling a function like
calculate_tax(100)without assigning it to a variable or using it in a print statement. The calculation happens, but the result is discarded immediately. - Code after return: Remember that
returnexits the function. If you put aprint()statement afterreturn, that code will never execute. - Confusing print with return:
printis for human eyes (debugging/logging).returnis for computer logic (passing data between parts of your app).
FAQ
Can a function return more than one value?
Yes. You can return multiple items as a tuple (e.g., return x, y), which you can then unpack into variables.
What happens if I don't use a return statement?
Python will return None by default. If you try to use that result in a calculation, you will likely trigger a TypeError.
Can I have multiple return statements in one function?
Yes. You might use an if statement to return early if a condition is met (e.g., if user is None: return False).
Recap
We have moved past simple output to functional data flow. By using the return keyword, you ensure your functions are no longer "black holes" that just print to the terminal, but rather useful tools that provide data for the rest of your application. You can now store these outputs in variables and chain them together to build complex processing pipelines.
Up next: We will apply these concepts to build a Statistics Processor that stores entries and calculates summary stats.
Work with me

Laravel REST API Development
Clean, secure, well-documented Laravel REST APIs — the backend engine for your app, mobile client, or SaaS. Built by an API specialist.

FilamentPHP Admin Panel & Dashboard Development
A powerful admin panel for your Laravel app — built with FilamentPHP so you can manage everything without touching the database.


