0

I want to make a function that can for example sum up all arguments:

def sum(#elements):
    return(a+...#all elements)

print(sum(1,3,4))
  • 1
    Possible duplicate of [Can a variable number of arguments be passed to a function?](https://stackoverflow.com/questions/919680/can-a-variable-number-of-arguments-be-passed-to-a-function) – xskxzr Apr 21 '18 at 04:24

2 Answers2

3

Put a * before the argument.

def my_sum(*args):
    total = 0
    for arg in args:
        total += arg
    return total

Now you can call it like my_sum(1, 2, 3, 4, 5).

Gabriel
  • 1,922
  • 2
  • 19
  • 37
2

Variable-length arguments

You may need to process a function for more arguments than you specified while defining the function. These arguments are called variable-length arguments and are not named in the function definition, unlike required and default arguments.

def sum( *vartuple ):
   total = 0
   for var in vartuple:
      total += var
   return total
Phd. Burak Öztürk
  • 1,727
  • 19
  • 29