Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Practicing With Python's sum()

00:00 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.

00:20 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.

00:34 Every day you get a sold units report from each point of sale. You need to systematically compute the cumulative sum to know how many units the whole company sold over the week.

00:47 To solve this problem, you can use sum().

01:01 By using start, you set an initial value to initialize the sum, which allows you to add successive units to that previously computed subtotal.

01:09 By the end of the week, you’ll have the company’s total count of sold units.

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.

02:00 The first part of the computation, where you add together the numbers, is the task for sum(). The next part of the operation, dividing by eight, uses the count of numbers in the sample.

02:10 To calculate the divisor, you can use len().

02:21 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.

02:38 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.

03:01 Here you use the walrus operator (:=) to store the number of data points in the variable num_points so that you won’t need to call len() again.

03:11 The return statement computes the sample’s arithmetic mean and sends it back to the calling code.

03:27 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 mean().

03:51 The 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 statistics.StatisticsError.

04:11 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.

04:39 To extract successive pairs of values from the input sequences, you can use zip(). Then you can use a generator expression to multiply each pair of values.

04:48 And finally, sum() can sum the products.

05:00 With 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 zip().

05:13 The final step is to add the products together using sum(). This code does work, but the dot product is defined for sequences of equal length.

05:23 So what happens when you provide sequences with different lengths?

05:33 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.

05:54 Here, dot_product() takes two sequences as arguments and returns their corresponding dot product. If the input sequences have different lengths, then the function raises a ValueError.

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.

06:29 In the next section of the course, you’ll take a look at how to use sum() to flatten a list of lists.

Become a Member to join the conversation.