How to Return a Tuple in Python?

Recently, at a webinar, someone asked about returning multiple values from a function in Python. In Python, we can return multiple values from a function using Python. In this tutorial, I will explain how to return a tuple in Python functions.

What is a Tuple in Python?

In Python, a tuple is an immutable ordered collection of elements. Tuples are defined using parentheses () and comma-separated values. Here’s an example of a tuple:

person = ("John", 25, "New York")

This tuple person contains three elements: a string "John", an integer 25, and another string "New York".

Return a Tuple from a Function in Python

A Python function can return a tuple by simply listing the values to be returned, separated by commas, with or without parentheses. Returning a tuple allows a function to have multiple outputs.

Here’s an example function that takes in a person’s name and age and returns them as a tuple:

def get_person_info(name, age):
    return name, age

person_info = get_person_info("Alice", 30)
print(person_info)  # Output: ('Alice', 30)

In this example, the get_person_info() function takes name and age as parameters and returns them as a tuple. The returned tuple is then assigned to the person_info variable.

I executed the above Python code, and you can see the exact output in the screenshot below:

Return a Tuple in Python

You can also explicitly use parentheses to return a tuple:

def get_location():
    city = "Los Angeles"
    state = "California"
    return (city, state)

location = get_location()
print(location)  # Output: ('Los Angeles', 'California')

The get_location() function returns a tuple containing the city and state variables.

Here is the exact output in the screenshot below:

Return a Tuple from a Function in Python

Check out How to Sort by the Second Element in a Tuple in Python?

Unpack Returned Tuples in Python

When a function returns a tuple, you can unpack its values into separate variables. This allows you to access the individual elements of the returned tuple conveniently. Tuple unpacking assigns each value in the returned tuple to separate variables.

Here’s an example:

def get_student_scores(name):
    math_score = 85
    science_score = 92
    return (name, math_score, science_score)

student_name, math, science = get_student_scores("Emily")
print(f"{student_name} scored {math} in Math and {science} in Science.")

Output:

Emily scored 85 in Math and 92 in Science.

In this example, the get_student_scores() function returns a tuple containing the student’s name and their scores in math and science. The returned tuple is unpacked into three variables: student_name, math, and science, which can then be used separately.

Check out How to Sort a Tuple in Python?

Return a Tuple in Python Examples

Let me show you more practical examples of returning tuples in Python functions.

Example 1: Returning Coordinates

Suppose you have a function that takes the coordinates of two points and returns the midpoint coordinates:

def calculate_midpoint(x1, y1, x2, y2):
    midpoint_x = (x1 + x2) / 2
    midpoint_y = (y1 + y2) / 2
    return midpoint_x, midpoint_y

point1 = (3, 5)
point2 = (7, 9)
midpoint = calculate_midpoint(*point1, *point2)
print(f"The midpoint is: {midpoint}")

Output:

The midpoint is: (5.0, 7.0)

The calculate_midpoint() function takes the coordinates of two points (x1, y1) and (x2, y2), calculates the midpoint coordinates, and returns them as a tuple. The returned tuple is then assigned to the midpoint variable.

I executed the above Python code, and you can see the exact output in the screenshot below:

How to Return a Tuple in Python

Example 2: Returning Multiple Aggregations

Let’s say you have a list of sales data, and you want to calculate the total sales, average sales, and maximum sale using a single function:

def analyze_sales(sales_data):
    total_sales = sum(sales_data)
    average_sales = total_sales / len(sales_data)
    max_sale = max(sales_data)
    return total_sales, average_sales, max_sale

sales = [1000, 1500, 800, 1200, 900]
total, average, max_sale = analyze_sales(sales)
print(f"Total sales: ${total}, Average sales: ${average:.2f}, Maximum sale: ${max_sale}")

Output:

Total sales: $5400, Average sales: $1080.00, Maximum sale: $1500

The analyze_sales() function takes a list of sales_data, calculates the total sales, average sales, and maximum sale, and returns them as a tuple. The returned tuple is unpacked into three variables: total, average, and max_sale, which are then used to display the sales analysis.

Conclusion

Returning tuples in Python functions provide a convenient way to return multiple values. By using tuple unpacking, you can easily access the individual elements of the returned tuple.

I hope this tutorial has clarified how to return tuples in Python functions and provided you with practical examples.

You may also like the following tutorials:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.