Last modified: Feb 05, 2026 By Alexander Williams
Python Sum Function Guide: Syntax & Examples
The sum() function is a built-in Python tool. It adds numbers together. This guide explains how to use it effectively.
You will learn the basic syntax. We will cover common use cases. Practical examples will make everything clear.
What is the Python sum() Function?
The sum() function calculates the total of items in an iterable. An iterable is an object you can loop over. Lists and tuples are common iterables.
This function is part of Python's standard library. You do not need to import anything. It is ready to use in any script.
Understanding basic Python function syntax is helpful before diving deeper.
Basic Syntax of sum()
The syntax for the sum() function is simple. It takes up to two arguments.
# sum() function syntax
sum(iterable, start)
The first argument is the iterable. It contains the numbers to add. The second argument is start. This value is added to the final total.
The start argument is optional. If you omit it, the default value is 0. The function then returns the sum of all items in the iterable.
How to Use sum() with a List
Using sum() with a list is very common. Let's look at a basic example.
# Summing a list of integers
numbers = [10, 20, 30, 40]
total = sum(numbers)
print(total)
100
The function adds 10, 20, 30, and 40. The result is 100. The start value was not used, so it defaulted to 0.
Using the Optional 'start' Parameter
The start parameter is useful. It lets you begin the summation from a specific number.
# Using the start parameter
numbers = [1, 2, 3]
total = sum(numbers, 10) # Start summing from 10
print(total)
16
Here, the calculation is 10 + 1 + 2 + 3. The result is 16. This is helpful for adding a base value.
Working with Other Iterables
sum() works with any iterable containing numbers. This includes tuples and sets.
# Summing a tuple
my_tuple = (5.5, 2.5, 1.0)
result = sum(my_tuple)
print(f"Tuple sum: {result}")
# Summing a set
my_set = {100, 200, 300}
result = sum(my_set)
print(f"Set sum: {result}")
Tuple sum: 9.0
Set sum: 600
The function behaves the same way. It iterates through the items and adds them up. The function return value is always a number.
Common Errors and How to Avoid Them
Beginners often encounter a few common errors. Knowing them will save you time.
Error 1: Non-numeric data. The sum() function only works with numbers.
# This will cause a TypeError
items = ['a', 'b', 'c']
total = sum(items) # ERROR!
You must ensure your iterable contains only integers or floats.
Error 2: Incorrect argument order. The iterable must be the first argument.
# Correct order
total = sum([1, 2], 3) # Result is 6 (1+2+3)
# Incorrect order will cause an error
# total = sum(3, [1, 2]) # TypeError
Always put the iterable collection first.
Practical Example: Calculating a Total Price
Let's use sum() in a real-world scenario. We will calculate a shopping cart total.
# Shopping cart example
cart_prices = [12.99, 5.49, 24.95, 3.99]
shipping_fee = 4.99
# Calculate total cost including shipping
total_cost = sum(cart_prices, shipping_fee)
print(f"Total cost: ${total_cost:.2f}")
Total cost: $52.41
Here, shipping_fee is the start parameter. It is added to the sum of all item prices.
Advanced Usage: Generator Expressions
You can combine sum() with a generator expression. This is powerful for conditional summing.
# Sum only even numbers from a list
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_sum = sum(num for num in numbers if num % 2 == 0)
print(f"Sum of even numbers: {even_sum}")
Sum of even numbers: 30
The generator filters the list. It only yields even numbers. The sum() function then adds them. This technique is memory efficient.
For more on handling multiple arguments, see our guide on Python function argument unpacking.
Conclusion
The Python sum() function is simple but powerful. It quickly adds numbers in any iterable.
Remember its two arguments: the iterable and the optional start value. Use it with lists, tuples, sets, and generators.
Avoid errors by ensuring your data is numeric. Place the iterable argument first in the function call.
Mastering sum() is a key step in learning Python. It helps write cleaner and more efficient code for data calculations.