Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Arbitrary number of arguments passing a tuple

from mysum import mysum

z = (2, 3, 5, 6)

mysum(z)

Output:

((2, 3, 5, 6),)
<class 'tuple'>
Traceback (most recent call last):
  File "/home/gabor/work/slides/python/examples/functions/sum_of_tuple.py", line 5, in <module>
    mysum(z)
  File "/home/gabor/work/slides/python/examples/functions/mysum.py", line 6, in mysum
    total += s
TypeError: unsupported operand type(s) for +=: 'int' and 'tuple'
from mysum import mysum

z = (2, 3, 5, 6)

print(mysum(*z))

Output:

(2, 3, 5, 6)
<class 'tuple'>
16