Returning multiple values from a function can be quite useful in various situations, such as performing mathematical calculations, string manipulations, or returning status information alongside the actual result.
Python provides several ways to return multiple values from a function, including using tuples, lists, dictionaries, and classes/objects. In this tutorial, we will explore these methods and provide examples of how to use them in your own Python code.
Method 1: Returning a Tuple
Tuples are immutable sequences in Python, which can store multiple values. You can return a tuple from a function by simply separating the return values with commas. Here’s an example of a function that calculates the area and perimeter of a rectangle:
1 2 3 4 5 6 7 8 9 |
def rectangle_area_perimeter(width, height): area = width * height perimeter = 2 * (width + height) return area, perimeter width = 5 height = 10 result = rectangle_area_perimeter(width, height) print("Area:", result[0], "Perimeter:", result[1]) |
Area: 50 Perimeter: 30
Method 2: Returning a List
Lists are mutable sequences in Python. You can return a list from a function just like a tuple but with the returned values enclosed in square brackets []
. Here’s the same example as before, but this time returning a list:
1 2 3 4 5 6 7 8 9 |
def rectangle_area_perimeter(width, height): area = width * height perimeter = 2 * (width + height) return [area, perimeter] width = 5 height = 10 result = rectangle_area_perimeter(width, height) print("Area:", result[0], "Perimeter:", result[1]) |
Area: 50 Perimeter: 30
Method 3: Returning a Dictionary
Dictionaries store key-value pairs, which can be particularly handy when you want to return multiple values with labels. Here’s our area and perimeter example, this time returning a dictionary:
1 2 3 4 5 6 7 8 9 |
def rectangle_area_perimeter(width, height): area = width * height perimeter = 2 * (width + height) return {'area': area, 'perimeter': perimeter} width = 5 height = 10 result = rectangle_area_perimeter(width, height) print("Area:", result['area'], "Perimeter:", result['perimeter']) |
Area: 50 Perimeter: 30
Method 4: Returning an Object
Sometimes, it might be more suitable to return an object that represents the values you want to return, particularly if they are complex or need to be processed further. Here’s an example using a class to represent a rectangle:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
class Rectangle(): def __init__(self, width, height): self.width = width self.height = height def area(self): return self.width * self.height def perimeter(self): return 2 * (self.width + self.height) def rectangle_area_perimeter(width, height): rect = Rectangle(width, height) return rect width = 5 height = 10 rectangle = rectangle_area_perimeter(width, height) print("Area:", rectangle.area(), "Perimeter:", rectangle.perimeter()) |
Area: 50 Perimeter: 30
Conclusion
In this tutorial, we have learned about four different ways to return multiple values from a Python function using tuples, lists, dictionaries, and objects. Depending on the requirements of your specific case, you can choose the method that best suits your needs and provides better readability and maintainability to your code.