How to Return Multiple Values from a Python Function

This code demonstrates how a Python function can return multiple values as a tuple, and how to unpack that tuple into individual variables.

Easy Python 3.6+ Aug 9, 2026 Functions & basics 16 views 0 copies

Python code

12 lines
Python 3.6+
def get_user_stats(name, score, level):
    """Return multiple values as a tuple."""
    return name, score, level

if __name__ == "__main__":
    result = get_user_stats("Alice", 95, 3)
    print(result)
    print(type(result))
    
    # Unpacking into individual variables
    player_name, player_score, player_level = result
    print(f"{player_name}: score={player_score}, level={player_level}")

Output

stdout
('Alice', 95, 3)
<class 'tuple'>
Alice: score=95, level=3

How it works

In Python, when you list values separated by commas after return, they are automatically packed into a tuple. The return name, score, level statement returns a tuple (name, score, level). Functions that return a tuple allow you to convey multiple related pieces of data without creating a custom class. The unpacking assignment player_name, player_score, player_level = result splits the tuple into individual variables, making the code more readable. This pattern works because the number of variables on the left matches the tuple length.

Common mistakes

  • Forgetting that the return type is a tuple and trying to treat it as a single value.
  • Trying to unpack into a different number of variables than the tuple length, which raises a ValueError.
  • Using a mutable list instead of a tuple when immutability is desired.

Variations

  1. Use a namedtuple from the collections module for named fields.
  2. Use a dataclass for more complex return data.
  3. Return a dictionary to have named access to the values.

Real-world use cases

  • A function that returns both the result and an error code to handle success/failure.
  • A data processing function that returns the updated record and the number of changes made.
  • A business logic function that returns the order total and the calculated tax for invoice generation.

Sponsored

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Functions & basics

Related tutorials and quizzes for this topic.