TNS
VOXPOP
404 - VOXPOP NOT FOUND
Give use a minute to figure out what's going on here ...
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:


The output of the above would be:

Total sales amount: 24800.

What we did above was define a variable to use for the starting value and then plug it in for the total_sales summation. We could always skip the initial_value variable and hard code the start value like this:


It’s also possible to use sum() while using input, but it’s a bit trickier.

What we have to do is first accept input for our variables with the following:


Next, we define a new variable from the user input like so:


We can then total the inputted numbers with the sum() function:


Finally, we print it out:


The entire code looks like this:


We could also use sum() to calculate the average of numbers. This also requires the len() function, which takes an object as an argument and returns the length of that object. This code looks like this:


What the above does is devise the sum of the numbers from num = sum(numbers) by the length of the list (which is 4). So 25 divided by 4 equals 6.25.

The Python sum() function will come in very handy as you continue your journey with this widely used language.

Group Created with Sketch.
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.