Menu Close

How to return two values from a function in Python?

Sometimes, we want to return two values from a function in Python.

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

How to return two values from a function in Python?

To return two values from a function in Python, we can return a tuple.

For instance, we write

def select_choice():
    # ...
    return i, card

my_i, my_card = select_choice()

to create the select_choice function that returns the i and card variables after putting them in a tuple.

Then we call select_choice and unpack the values from the tuple by assigning them to variables separated by commas.

Conclusion

To return two values from a function in Python, we can return a tuple.

Posted in Python, Python Answers