[ Web Proxy ]
URL:
Viewing: https://www.pythonbyexample.dev/examples/multiple-return-values [Back]  [Original]

Multiple Return Values Python By Example Skip to main content Python By ExampleJourneysAbout
All examplesPython docs reference

Functions

Multiple Return Values

Python returns multiple values by returning a tuple and unpacking it.

Returning values separated by commas returns one tuple. The tuple is visible if the caller stores the result directly.

Source

def divide_with_remainder(total, size):
    quotient = total // size
    remainder = total % size
    return quotient, remainder

result = divide_with_remainder(17, 5)
print(result)

Output

(3, 2)
def f(): return a, b(a, b)x, yA function returning multiple values really returns one tuple; the caller unpacks it into named bindings.

Callers usually unpack the tuple immediately or soon after. The names at the call site document what each position means.

Source

boxes, leftover = result
print(boxes)
print(leftover)

Output

3
2

Notes

See also

Args and KwargsClosures

Run the complete example

code:

Expected output

(3, 2)
3
2

Execution time appears here after you run the example.

adewale/pythonbyexample Python 3.13 docs Privacy
Web Proxy Viewer  |  New URL  |  Original Page