TNS
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
✓
Angular
0%
✓
Astro
0%
✓
Svelte
0%
✓
Vue.js
0%
✓
Other
0%
✓
I only use React
0%
✓
I don't use JavaScript
0%
Python / Software Development

What Is Python’s sum() Function and How Do You Use It?

This tutorial shows you how to use Python's integrated sum() function, which is a Pythonic way to sum a list of numeric values.
Aug 11th, 2024 6:00am by
Featued image for: What Is Python’s sum() Function and How Do You Use It?
Featured image via Unsplash+.

In Python, you could always do something like this:


The above code is simple: It adds 3 to 5 and prints the total. You could even do something like this:


Once again, Python will total and print the value. And given that adding several numbers together is a widely used step in computations, it’s pretty handy that Python can do this.

You could even use a for loop to sum a string of numbers together like this:


The above would output the same value as the two-line code above it. In the above code, total is initialized to 0, and then the variable acts as an accumulator as it iterates through the loop. We could also create a function that will do the same and can be reused throughout the application. Such a function might look like this:


However, there’s a built-in function that’s much better to use. The function in question is sum(). One of the primary reasons for using this function is readability. Instead of having to write for loops or recursions, sum() takes care of all that for you.

The sum() function wasn’t introduced until Python 2.3 and serves as a Pythonic solution to summing numbers.

Using sum() is very easy. For instance:


Pretty simple.

You could also use sum() like this:


The sum() function can be used with lists, tuples, sets, ranges and dictionaries.

Let me show you how each might look:

  • List: sum([3, 5, 7, 10])
  • Tuple: sum((3, 5, 7, 10))
  • Set: sum({3, 5, 7, 10})
  • Range: sum(range(3, 10))
  • Dictionary: sum({3: “three”, 5: “five”, 7: “seven”, 10: “ten”})

With the exception of the range, all of the above will total 25. The range, on the other hand, will total 42.

You could also get a bit more fancy with your math. For example, let’s say you want to compute the sum of the squares from a range of values. That might look something like this:


The total from the above would be 285.

The sum() function has two arguments: the required iterable (the numbers to be iterated through) and the optional start. The start argument comes in very handy. Let’s say you want to add a list of iterables together but you want to start out with a predefined starting value. Without the start argument, the starting value is always assumed to be 0. Take a look at the following line:


The total is 25 because our starting value is the default, 0. But what if we want our starting value to be, say, 100. For that, you could use the start argument like this:


Our total is now 125.

But why not just add the 100 to the list, like so:


You could do that and it would work just fine. But let me give you an example where using the start argument illustrates why it can be necessary.

Let’s say you want to add the total amount of a sale but you want to start off by giving a discount. For example, the discount might be $200.00 off. How would you do that? With the start argument.

Let’s say you have a list of dollar amounts that looks like this:


You could set an initial value like this:


Finish up the code like so: