Python is a popular programming language that is widely used for developing a variety of applications. One of the features that makes Python stand out is its ability to return multiple values from a function. This feature enables developers to create functions that return more than one value, making it easier to work with complex data structures and improve code readability.

In this article, we will discuss how to return multiple values from a function in Python.

What are Multiple Return Values?

In Python, a function can return a single value or multiple values. When a function returns multiple values, it returns them as a tuple. A tuple is a collection of elements, similar to a list or an array. However, tuples are immutable, which means that their elements cannot be modified once they are created.

Example to Return Multiple Values

To return multiple values from a function in Python, you need to define the function and use the return statement to return the values as a tuple. Here’s an example:

def get_name_and_age():

    name = “John”

    age = 30

    return name, age

result = get_name_and_age()

print(result)

In this example, we have defined a function called get_name_and_age() that returns two values, name and age. We use the return statement to return the values as a tuple. When we call the function and assign the result to a variable called result, we get a tuple containing the values (“John”, 30). We then print the result to the console using the print() function.

This program will output the following result:

Output

("John", 30)

Assign Return Values to Variables

We can also assign the values returned by the function to separate variables like this:

def get_name_and_age():

    name = “John”

    age = 30

    return name, age

name, age = get_name_and_age()

print(name)

print(age)

In this example, we have assigned the values returned by the function to two separate variables, name and age. When we call the function, the values are returned as a tuple, and Python automatically unpacks the tuple and assigns the values to the variables. We can then print the values to the console using the print() function.

This program will output the following result:

Output

John 30

Benefits of Returning Multiple Values

Returning multiple values from a function in Python offers several benefits, including:

  • Improved readability: By returning multiple values from a function, you can make your code more readable and easier to understand. This is particularly useful when working with complex data structures.
  • Reduced code complexity: By returning multiple values from a function, you can reduce the complexity of your code and make it easier to maintain.
  • Increased flexibility: By returning multiple values from a function, you can provide more flexibility to other parts of your code, allowing them to perform more complex operations.

Conclusion

Returning multiple values from a function in Python is a useful feature that can make your code more readable and easier to work with. By using the return statement to return a tuple containing multiple values, you can improve code clarity and reduce complexity. We hope this article has provided you with a clear understanding of how to return multiple values from a function in Python.