Skip to Content
Course content

71: Return Values and Multiple Returns

Click on the "Edit" button in the top corner of the screen to edit your slide content.

I've noticed a pattern with a lot of developers when they first hit the return statement in Python. They see a line like return x, y, z and they tell themselves, "Great, this function returns three different values."

The "Multiple Values" Illusion

It feels intuitive. You list three variables, you get three variables back. But here is the reality: a Python function can only ever return one single object.

Let's look at a concrete example. Imagine we're writing a tool to handle basic coordinates for a game map. You want to get both the X and Y coordinates from a function.

def get_player_position():
    x = 150
    y = 300
    return x, y

result = get_player_position()
print(result)
# Output: (150, 300)

If you were actually returning two separate things, the output wouldn't have those parentheses. Those parentheses are the tell. When you write return x, y, Python is silently wrapping those values into a single tuple. You aren't returning two integers; you are returning one tuple containing two integers. I'll be honest—this "magic" is helpful, but if you don't realize it's happening, you'll run into bugs the moment you try to perform math on the result variable and find out it's a tuple instead of a number.

Unpacking the Tuple for Clean Code

Now, since we know Python is just handing us a tuple, we have two ways to handle it. You can use indexing (which is ugly and confusing), or you can use sequence unpacking. This is where the "illusion" of multiple returns becomes a powerful tool for your code's readability.

# The clunky way
pos = get_player_position()
x_coord = pos[0]
y_coord = pos[1]

# The "Pythonic" way
x_coord, y_coord = get_player_position()

In the second example, Python takes the tuple returned by the function and "unpacks" its contents into the two variables on the left. It's a syntactic shortcut. I always recommend this approach because it tells anyone reading your code exactly what the function is providing without them having to go hunt down the function definition to see what pos[0] actually represents.

When Tuples Aren't Enough

Tuples are great for two or three values. But as a professional tip: once you find yourself returning four, five, or six values, stop. Returning a massive tuple is a recipe for disaster because it's incredibly easy to swap the order of two variables during unpacking, and Python won't warn you that you've accidentally put the "player_health" value into the "player_score" variable.

When the return list gets long, I shift to returning a dictionary. This gives each returned value a label, making the code self-documenting:

def get_user_profile():
    # Imagine this comes from a database
    return {
        "username": "coder_99",
        "email": "dev@example.com",
        "join_date": "2023-01-15",
        "access_level": "Admin"
    }

user = get_user_profile()
print(user["username"]) # No guessing which index is the username!



📋 Practical Task

The Text Statistics Analyzer

Build a function called analyze_text that takes a single string as an argument. The function should calculate three different metrics:

  • The total number of characters (including spaces).
  • The number of words (assume words are separated by spaces).
  • The number of uppercase letters.
Your function must return these three values using Python's tuple-packing syntax.

Outside the function, call analyze_text with a sentence of your choice, unpack the returned values into three descriptively named variables, and print them in a user-friendly format (e.g., "Total characters: 42").
Rating
0 0

There are no comments for now.

to be the first to leave a comment.