How do I calculate the sum of the elements of a Python list?

To calculate the sum of the elements of a Python list, you can use the built-in sum(iterable, start) method. The sum() method takes an iteration as its first argument and an optional starting number as the second argument sums the items and returns the result. The optional "start" argument adds the specified value to the result (defaults to 0). If the elements are not numbers, a TypeError exception will be thrown. In this Python example, we calculate the sum of the list elements using the sum() method and add an initial value to the result, passing it as the second argument. Click Execute to run Python List Sum Example online and see the result.
Loading...