In Python, functions can return multiple values as a tuple. This feature allows you to return multiple values from a function without using global variables or lists. In this tutorial, we will learn how to return multiple values from a position in Python.
Python Program to Return Multiple Values From a Function
To return multiple values from a function, you need to use the return statement and separate each value with a comma. The returned values will be packed into a tuple. Here is an example of a function that returns two values:
def name(): return "Multiple", "Values"
To access the values in the returned tuple, you can use the unpacking operator, =. For example, the following code will print the returned values individually:
name_1, name_2 = name() print(name_1, name_2)
If you want to see the full tuple, you can simply print the function call:
('Multiple', 'Values') Multiple Values
As you can see, the first print statement shows the full tuple, and the second print statement shows the individual values.
It’s worth noting that returning multiple values from a function is not always the best solution. In some cases, it may be better to return a single object that contains all the values you want to return. This is known as an object-oriented approach and is a more scalable solution as your codebase grows.
Here is the full Python program Return Multiple Values From a Function.
# Return Multiple Values From a Function in python def name(): return "Multiple","Values" # print the tuple with the returned values print(name()) # get the individual items name_1, name_2 = name() print(name_1, name_2)
('Multiple', 'Values') Multiple Values
For example, instead of returning multiple values, you can define a class to store all the values:
class Name: def __init__(self, name_1, name_2): self.name_1 = name_1 self. name_2 = name_2 def get_names(): return Name("Multiple", "Values")
And access the values like this:
names = get_names() print(names.name_1, names.name_2)
The output will be:
In this example, we have created a class Name that holds the values we want to return. The get_names function returns an instance of the Name class, which can be accessed like any other object.
Advantages of Returning Multiple Values from a Function:
- Concise Code: Returning multiple values from a function can make your code more concise and readable. You don’t have to use global variables or lists to store intermediate values, and you can return all the values you need in a single function call.
- Easy to Use: Accessing the values returned by a function is easy, and you can use the unpacking operator to unpack the values directly into separate variables.
- Reusable Code: By returning multiple values, you can make your functions more reusable. For example, you can write a function that returns multiple values and use it in multiple parts of your code, reducing duplicated code.
Disadvantages of Returning Multiple Values from a Function:
- Confusing Output: If the values returned by a function are not well-documented, it can be difficult to understand what they represent. This can make it harder to maintain and update your code.
- Limited Functionality: If you need to return more than a few values, returning multiple values can become unwieldy. In these cases, it may be better to return a single object that contains all the values you want to return.
- Debugging Challenges: Debugging code that returns multiple values can be challenging, especially if you need to trace the values through multiple function calls.
In conclusion, returning multiple values from a function is a powerful feature that can make your code more concise and reusable. However, it also has its disadvantages, so it’s essential to use it judiciously and choose the right approach for your specific requirements.
For More Python Programming Exercises and Solutions check out our Python Exercises and Solutions