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.

Understanding the Summation Problem

00:00 Understanding The Summation Problem. Summing numeric values together is a fairly common problem in programming. For example, let’s say you have a list of numbers and want to add them together to compute their total sum. With standard arithmetic, you do something like this.

00:17 As far as the math goes, this expression is fairly straightforward. It walks you through a short series of additions until you find the sum of all the numbers. It’s possible to do this particular calculation by hand, but you can imagine some other situations where it wouldn’t be.

00:33 If you have a particularly long list of numbers, adding by hand can be inefficient and error-prone. What happens if you don’t even know how many items are in the list? Finally, imagine a scenario where the number of items you need to add changes dynamically or unpredictably. In situations like these, whether you have a long or short list of numbers, Python can be quite useful to solve summation problems.

00:59 If you want to sum the numbers by creating your own solution from scratch, then you can try using a for loop as seen on-screen.

01:09 Here you first create total and initialize it to 0. This variable works as an accumulator in which you store intermediate results until you get the final one.

01:20 The loop iterates through the numbers and updates total by accumulating each successive value using an augmented assignment. You can also wrap the for loop in a function.

01:33 This way, you can reuse the code for different lists. In sum_numbers(), you take an iterable as an argument, specifically a list of numeric values, and you return the total sum of the values in the input list.

01:47 The for loop is the same one that you saw previously.

01:54 If the input list is empty, then the function returns 0.

02:03 You can also use recursion instead of iteration. Recursion is a functional programming technique where a function is called within its own definition. In other words, a recursive function calls itself in a loop.

02:22 When you define a recursive function, you take the risk of running into an infinite loop. To prevent this, you need to define both a base case that stops the recursion and a recursive case to call the function and start the implicit loop. Here, the base case implies that the sum of a zero-length list is 0.

02:42 The recursive case implies that the total sum is the first value, numbers[0], plus the sum of the rest of the values, numbers[1:].

02:51 Because the recursive case uses a shorter sequence on each iteration, you expect to run into the base case where numbers is a zero-length list.

03:00 As a final result, you get the sum of all the items in your input list, numbers.

03:09 Another option to sum a list of numbers in Python is to use reduce() from functools. To get the sum of a list of numbers, you can pass either operator.add or an appropriate lambda function as the first argument to reduce(). You call reduce with a reduction, or folding, function along with an iterable as arguments.

03:29 Then reduce() uses the function to process the iterable and returns a single cumulative value. Here the reduction function is add(), which takes two numbers and adds them together.

03:41 The final result is the sum of the numbers in the input iterable. Note that reduce() raises a TypeError when you call it with an empty iterable.

03:54 Here, the reduction function is a lambda function that returns the addition of two numbers, leading to the same result as add().

04:05 Since summations like these are commonplace in programming, coding and new function, every time you need to sum some numbers is a lot of repetitive work.

04:14 Additionally, using reduce() isn’t the most readable solution available to you. Python provides a dedicated built-in function to solve this problem.

04:23 Appropriately, the function is called sum(). As it’s a built-in function, you can use it directly in your code without importing anything, and you’ll start doing exactly that in the next section of the course.

Become a Member to join the conversation.