11
Lesson 11
Returning multiple values
Returning multiple values from a function is a powerful feature that allows you to package and return more than one result from a function call. This can be particularly useful in scenarios where a function needs to compute and provide multiple pieces of related information.
How It Works
In Python, you can return multiple values from a function by separating them with commas. When a function returns multiple values, they are returned as a tuple. You can also unpack these values into separate variables when calling the function.
How It Works
In Python, you can return multiple values from a function by separating them with commas. When a function returns multiple values, they are returned as a tuple. You can also unpack these values into separate variables when calling the function.
def calculate_statistics(numbers): total = sum(numbers) # Calculate the sum of the list count = len(numbers) # Calculate the number of elements average = total / count if count > 0 else 0 # Calculate the average return total, count, average # Returning multiple values # Example usage stats = calculate_statistics([10, 20, 30, 40, 50]) # Calling the function print(stats) # Output: (150, 5, 30.0) # Unpacking the returned values total, count, average = stats print("Total:", total) # Output: Total: 150 print("Count:", count) # Output: Count: 5 print("Average:", average) # Output: Average: 30.0
Explanation:
- The function calculate_statistics(numbers) takes a list of numbers as input.
- It calculates the total, count, and average of the numbers.
- The function returns three values: total, count, and average. These are separated by commas, making it clear that multiple values are being returned.
- When the function is called, Python creates a tuple containing the three returned values, which in this case is (150, 5, 30.0).
- The returned tuple can be unpacked into separate variables (total, count, and average), allowing you to use each value independently.
Using returned values:
Returning multiple values can make your functions more versatile and reduce the need for complex data structures like dictionaries or custom classes when simple tuples suffice. Here are some scenarios where returning multiple values is beneficial:
- Mathematical computations: When performing calculations that yield several results, such as sums, averages, or maximum/minimum values.
- Coordinate systems: Returning multiple coordinates (x, y) from functions dealing with geometric computations.
- Data processing: Functions that need to return both processed data and metadata, such as counts or statuses.
def divide_and_remainder(a, b): quotient = a // b # Integer division remainder = a % b # Remainder of division return quotient, remainder # Return both results # Example usage q, r = divide_and_remainder(10, 3) # Unpacking the returned values print("Quotient:", q) # Output: Quotient: 3 print("Remainder:", r) # Output: Remainder: 1
Summary
- Returning multiple values: Functions in Python can return multiple values by separating them with commas, resulting in a tuple.
- Unpacking values: The returned tuple can be unpacked into individual variables for easier access and use.
- Versatility: This feature enhances code clarity and reusability, making it easier to handle functions that naturally produce multiple outputs.
Using this feature wisely can simplify your code and enhance its readability, especially when dealing with complex calculations or data processing tasks.