When can I use sum function -This gives error


def add_function(x,y):
return sum(x,y)


sum() is a built-in function which gives you the sum for an iterable, like a list.

To just add numbers together, use +


See the official documentation for the built-in sum function.

Accordingly, if you wanted to use it to add x and y and return the result, you could place them in a list, as follows …

  return sum([x, y])

… or in a tuple, like this …

  return sum((x, y))

However, @stetim94’s solution makes the most sense.

The official Python web site is a good place to go for documentation.

See Python: Numeric Types for information on numeric operators and several functions.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.