I am required to use the sum()
function in order to sum the values in a list. Please note that this is DISTINCT from using a for
loop to add the numbers manually. I thought it would be something simple like the following, but I receive TypeError: 'int' object is not callable
.
numbers = [1, 2, 3]
numsum = (sum(numbers))
print(numsum)
I looked at a few other solutions that involved setting the start parameter, defining a map, or including for
syntax within sum()
, but I haven't had any luck with these variations, and can't figure out what's going on. Could someone provide me with the simplest possible example of sum()
that will sum a list, and provide an explanation for why it is done the way it is?
In the last answer, you don't need to make a list from numbers; it is already a list:
numbers = [1, 2, 3]
numsum = sum(numbers)
print(numsum)
numbers = [1, 2, 3]
numsum = sum(list(numbers))
print(numsum)
This would work, if your are trying to Sum up a list.
Source: Stackoverflow.com