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 return a tuple.

For instance, we write

def f():
    return True, False
x, y = f()

to define the f function that returns a tuple with True and False in it.

And then we unpack the returned values returned from f with

x, y = f()

Therefore x is True and y is False.

Conclusion

To return multiple values from a function with Python, we can return a tuple.

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 *