Categories
Python Answers

How to return multiple values from a function with Python?

Spread the love

Sometimes, we want to return multiple values from a function with Python.

In this article, we’ll look at how to return multiple values from a function with Python.

How to return multiple values from a function with Python?

To return multiple values from a function with Python, we can use the return statement with values separated by commas.

For instance, we write:

def foo():
    return True, False


x, y = foo()
print(x)
print(y)

to define the foo function that returns True and False.

And then we call foo and assign the returned values to x and y.

Therefore, we see:

True
False

printed.

Conclusion

To return multiple values from a function with Python, we can use the return statement with values separated by commas.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *