Practicing With Python's sum()
Practicing With Python’s
sum(). So far, you’ve learned the basics of working with
sum(). You’ve learned how to use this function to add numeric values together and also to concatenate sequences such as lists and tuple. In this section, you’ll look at some more examples of when and how to use
sum() in your code.
The first example you’ll code shows how to take advantage of the
start argument for summing cumulative lists of numeric values. Let’s say you are developing a system to manage the sales of a given product at several different points of sale.
01:34 Let’s say you need to calculate the arithmatic mean of a sample of numeric values. The arithmetic mean, also known as the average, is the total sum of the values divided by the number of values or data points in the sample.
01:48 If you have the sample seen on-screen and you want to calculate the arithmetic mean by hand, then you can solve this operation. If you want to speed this up using Python, you can break it down into two parts.
Here, the call to
sum() computes the total sum of the data points in the sample. Next, you use
len() to get the number of data points. Finally, you perform the required division to calculate the sample’s arithmetic mean.
In practice, you may want to turn this code into a function with some additional features, such as a descriptive name and a check for empty samples. Inside
average(), you first check if the input sample has any data points. If not, then you raise a
ValueError with a descriptive message.
Note that computing the mean of a sample of data is a common operation in statistics and data analysis. The Python standard library provides a convenient module called
statistics to approach these kinds of calculations. In the
statistics module, you’ll find a function called
statistics.mean() function has a very similar behavior to the
average() function you coded earlier. When you call
mean() with a sample of numeric values, you get the arithmetic mean of the input data. When you pass an empty list to
mean(), you get a
Another problem you can solve using
sum() is finding the dot product of two equal-length sequences of numeric values. The dot product is the algebraic sum of products of every pair of values in the input sequences. For example, if you have the sequences (1, 2, 3) and (4, 5, 6), then you can calculate their dot product by hand using addition and multiplication.
zip(), you generate a list of tuples with the values from each of the input sequences. The generator expression loops over each tuple while multiplying the successive pairs of values previously created by
In that case,
zip() ignores the extra values from the longest sequence, which leads to an incorrect result. To deal with this possibility, you can wrap the call to
sum() in a custom function and provide a proper check for the length of the input sequences.
06:13 Embedding the functionality in a custom function allows you to reuse the code. It also gives you the opportunity to name the function descriptively so that your user knows what the function does just by reading its name.
Become a Member to join the conversation.