Getting Started With Python's sum()
Getting Started With Python’s
sum(). Readability is one of the most important principles behind Python’s philosophy. Visualize what you’re asking a loop to do when summing a list of values.
00:12 You want it to loop over some numbers, accumulate them in an intermediate variable, and return the final sum. However, you can probably imagine a more readable version of summation that doesn’t need a loop. You want Python to take some numbers and sum them together.
Now think about how
reduce() does summation. Using
reduce() is arguably less readable and less straightforward than even the loop-based solution.
This is why
sum() was added in Python 2.3 to provide a Pythonic solution to the summation problem, which is now the preferred syntax for summing a list of values.
This is much neater. It reads like plain English and clearly communicates the action you’re performing on the input list. Using
sum() is much more readable than a
for loop or a
sum() doesn’t raise a
TypeError when you provide an empty iterable. Instead, it returns
You call some with the following two arguments.
iterable is a required argument that can hold any Python iterable. The iterable typically contains numeric values, but can also contain lists, or tuples.
start is an optional argument that can hold an initial value. This value is then added to the final result, and it defaults to
start plus the values in the iterable from left to right. The optional argument
start can accept a number, list, or tuple, depending on what’s passed to the iterable. It can’t take a string.
Accepting any Python iterable as its first argument makes
sum() generic, reusable, and polymorphic. Because of this feature, you can use
sum() with lists, tuples, sets,
range objects, and dictionaries, summing the keys both implicitly and explicitly.
02:35 You can also sum the values of a dictionary.
In all these examples,
sum() computes the arithmetic sum of all the values in the input iterable regardless of their types.
You can also use
sum() with a list comprehension as an argument. Here’s an example that computes the sum of the squares of a range of values.
Python 2.4 added generator expressions to the language. Again,
sum() works as expected when you use a generator expression as an argument.
03:19 This shows one of the most Pythonic techniques to approach the summation problem. It provides an elegant, readable, and efficient solution in a single line of code.
The second, optional argument,
start, allows you to provide a value to initialize the summation process. This argument is handy when you need to process cumulative values sequentially and can be provided as a positional argument or a keyword argument.
In both cases, you provide an initial value of
100 to start. The net effect is that
sum() adds this value to the cumulative sum of the values in the input iterable.
Note that while you can provide
start as a positional argument or a keyword argument, the second option is much more explicit and readable.
If you don’t provide a value to
start, it defaults to zero. This default value ensures the expected behavior of returning the total sum of the input values. In the next section of the course, you’ll work further with
sum(), looking at summation of numeric values and concatenation of sequences.
Become a Member to join the conversation.