Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Summing Values the Pythonic Way With sum()
Python’s built-in function sum() is an efficient and Pythonic way to sum a list of numeric values. Adding several numbers together is a common intermediate step in many computations, so sum() is a pretty handy tool for a Python programmer.
As an additional and interesting use case, you can concatenate lists and tuples using sum(), which can be convenient when you need to flatten a list of lists.
In this tutorial, you’ll learn how to:
- Sum numeric values by hand using general techniques and tools
- Use Python’s
sum()to add several numeric values efficiently - Concatenate lists and tuples with
sum() - Use
sum()to approach common summation problems - Use appropriate values for the arguments in
sum() - Decide between
sum()and alternative tools to sum and concatenate objects
This knowledge will help you efficiently approach and solve summation problems in your code using either sum() or other alternative and specialized tools.
Free Bonus: Click here to get a Python Cheat Sheet and learn the basics of Python 3, like working with data types, dictionaries, lists, and Python functions.
Understanding the Summation Problem
Summing numeric values together is a fairly common problem in programming. For example, say you have a list of numbers [1, 2, 3, 4, 5] and want to add them together to compute their total sum. With standard arithmetic, you’ll do something like this:
1 + 2 + 3 + 4 + 5 = 15
As far as math goes, this expression is pretty 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 imagine some other situations where it might not be so possible. 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.
If you want to sum the numbers by creating your own solution from scratch, then you can try using a for loop:
>>> numbers = [1, 2, 3, 4, 5]
>>> total = 0
>>> for number in numbers:
... total += number
...
>>> total
15
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. The loop iterates through numbers and updates total by accumulating each successive value using an augmented assignment.
You can also wrap the for loop in a function. This way, you can reuse the code for different lists:
>>> def sum_numbers(numbers):
... total = 0
... for number in numbers:
... total += number
... return total
...
>>> sum_numbers([1, 2