Getting Started With Python's sum()
00:00
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.
00:31
Now think about how reduce()
does summation. Using reduce()
is arguably less readable and less straightforward than even the loop-based solution.
00:39
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.
00:54
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 reduce()
call.
01:05
And unlike reduce()
, sum()
doesn’t raise a TypeError
when you provide an empty iterable. Instead, it returns 0
.
01:18
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 0
.
01:41
Internally, sum()
adds 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.
01:56
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.
02:43
In all these examples, sum()
computes the arithmetic sum of all the values in the input iterable regardless of their types.
02:53
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.
03:06
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.
03:32
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.
03:55
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.
04:09
Note that while you can provide start
as a positional argument or a keyword argument, the second option is much more explicit and readable.
04:17
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.