Back to Blog
Lesson 16 of the Python: Programming from Zero with Python course
PythonAugust 2, 20264 min read

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.

pythonprogrammingfunctionsreturnbackendbeginners
Creative flat lay of gadgets and 'Beginners Guide' text on wooden table.

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:

  1. The function stops: Any code written after the return line inside that function is ignored.
  2. 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

Vivid, blurred close-up of colorful code on a screen, representing web development and programming.

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:

PYTHON
def 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:

PYTHON
def 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.

  1. Define get_sum(numbers_list).
  2. Inside, use the built-in sum() function to return the total.
  3. Call the function, store the result in a variable called total_sum.
  4. Divide total_sum by 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 return exits the function. If you put a print() statement after return, that code will never execute.
  • Confusing print with return: print is for human eyes (debugging/logging). return is 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.

Similar Posts