Python sum() Function Explained
The built-in sum() function adds numbers from an iterable such as a list, tuple, or range object.
It is one of the simplest ways to get a total in Python. Beginners often use it to add all numbers in a list, but it also has an optional start value and a few important limits.
Quick example
numbers = [1, 2, 3, 4]
total = sum(numbers)
print(total) # 10
Use sum() to add numbers from an iterable like a list or tuple.
What sum() does
sum() adds numeric values from an iterable.
Common beginner uses include:
- Adding numbers in a list
- Adding values in a tuple
- Adding numbers from
range() - Counting
Truevalues in a list of booleans
It returns the final total as a number.
Basic syntax
sum(iterable, start=0)
iterableis the group of values to addstartis an optional value added before the iterable values- Most of the time, beginners only need
sum(iterable)
If you are new to the word iterable, think of it as a group of values you can loop through, such as a list, tuple, or range() object.
Simple example
Here is a basic example with a list of integers:
numbers = [5, 10, 15]
total = sum(numbers)
print(total)
Output:
30
What happens here:
numberscontains three integerssum(numbers)adds them together- The result is stored in
total
Using the start value
The second argument lets you begin with an extra value before Python adds the iterable items.
numbers = [1, 2, 3]
total = sum(numbers, 10)
print(total)
Output:
16
This works like:
- Start at
10 - Add
1 - Add
2 - Add
3
This is useful when you already have a starting total.
What sum() can work with
sum() works with numeric values.
Integers
values = [1, 2, 3, 4]
print(sum(values))
Output:
10
Floats
prices = [2.5, 3.75, 1.25]
print(sum(prices))
Output:
7.5
If you need a refresher on numeric types, see Python numbers explained: int and float.
Values from range()
print(sum(range(1, 6)))
Output:
15
This adds 1 + 2 + 3 + 4 + 5.
Boolean values
Booleans can also be summed because True acts like 1 and False acts like 0.
results = [True, False, True, True]
print(sum(results))
Output:
3
This is a simple way to count how many values are True.
What sum() does not work with
sum() is for numeric addition. It is not for joining text or combining lists.
It does not join strings
This causes an error:
words = ["hello", "world"]
print(sum(words))
Use string joining instead:
words = ["hello", "world"]
print("".join(words))
Output:
helloworld
If you need to turn values into text first, see str() explained.
It does not combine lists
This also causes an error:
lists = [[1, 2], [3, 4]]
print(sum(lists))
sum() is not the right tool for combining lists.
It fails with mixed incompatible types
For example:
values = [1, 2, "3"]
print(sum(values))
This fails because Python cannot add integers and strings together.
If you have number strings such as "3", convert them first. See how to convert a string to an int in Python.
Common errors and fixes
Here are some of the most common beginner mistakes with sum().
Passing a single number instead of an iterable
This is wrong:
print(sum(5))
Why it fails:
5is a single integersum()expects an iterable, not one number
Use an iterable instead:
print(sum([5]))
Output:
5
Trying to sum strings
This is wrong:
values = ["1", "2", "3"]
print(sum(values))
Fix it by converting the strings to integers:
values = ["1", "2", "3"]
total = sum(int(x) for x in values)
print(total)
Output:
6
This kind of problem is related to TypeError with unsupported operand types.
Mixing numbers and strings
This is wrong:
values = [1, 2, "3", 4]
print(sum(values))
Fix it by making all values numeric:
values = [1, 2, "3", 4]
total = sum(int(x) for x in values)
print(total)
Output:
10
Using sum() to join text
This is wrong:
letters = ["P", "y", "t", "h", "o", "n"]
print(sum(letters))
Use join() instead:
letters = ["P", "y", "t", "h", "o", "n"]
print("".join(letters))
Output:
Python
If you see string-related errors while building text, you may also want to read TypeError: sequence item 0 expected str instance.
sum() vs manual loop
sum() is often better when you only need the total.
Using sum()
numbers = [4, 7, 9]
total = sum(numbers)
print(total)
Using a manual loop
numbers = [4, 7, 9]
total = 0
for number in numbers:
total += number
print(total)
Both produce the same result.
Why sum() is often better:
- Shorter code
- Easier to read
- Clear intent: you want a total
Why a manual loop is sometimes better:
- You need to skip some values
- You need extra checks
- You want to change values before adding them
For example, if you only want certain items, you might first filter a list in Python and then use sum() on the filtered result.
Common mistakes
These are the most common causes of problems with sum():
- Passing a number instead of an iterable, such as
sum(5) - Trying to sum strings like
['1', '2', '3']without converting them - Using
sum()to join text instead of using stringjoin() - Mixing numbers and strings in the same iterable
If sum() is failing, these quick checks can help:
print(values)
print(type(values))
print([type(x) for x in values])
print(sum([int(x) for x in values]))
What these checks do:
print(values)shows the actual dataprint(type(values))shows whether the main object is a list, tuple, or something elseprint([type(x) for x in values])shows the type of each itemprint(sum([int(x) for x in values]))tests whether conversion to integers fixes the problem
FAQ
What does sum() return in Python?
It returns the total of the numeric values in an iterable.
Can sum() add strings in Python?
No. Use ''.join(...) or 'separator'.join(...) for strings.
Can sum() work with floats?
Yes. It can add both integers and floats.
What is the second argument in sum()?
It is the optional start value added before the iterable values are summed.
Why does sum() give a TypeError?
Usually because one or more values are not numbers, or because the argument is not an iterable.