The sum() function is used to calculate the total of numeric values in an iterable such as a list, tuple, or set.
It is commonly used for:
- Adding numbers
- Calculating total marks
- Calculating total salary
- Finding totals after
filter()ormap() - Calculating averages
- Working with numerical data
1. Basic Example
numbers = [10, 20, 30, 40, 50]
result = sum(numbers)
print(result)Output:
1502. sum() with a Tuple
numbers = (10, 20, 30, 40)
print(sum(numbers))Output:
1003. sum() with a Set
numbers = {10, 20, 30, 40}
print(sum(numbers))Output:
1004. sum() with a Range
numbers = range(1, 6)
print(sum(numbers))Output:
15The values are:
1 + 2 + 3 + 4 + 5 = 155. sum() with Multiple Values
sum() expects an iterable rather than separate arguments.
Correct:
numbers = [10, 20, 30]
print(sum(numbers))Incorrect:
# sum(10, 20, 30)6. Using the start Parameter
The syntax is:
sum(iterable, start)Example:
numbers = [10, 20, 30]
result = sum(numbers, 100)
print(result)Output:
160Python calculates:
100 + 10 + 20 + 30 = 1607. Default start Value
If start is not provided, Python uses 0.
numbers = [10, 20, 30]
print(sum(numbers))Internally:
0 + 10 + 20 + 30Result:
608. Sum Positive Numbers
numbers = [10, -5, 20, -3, 30]
result = sum(
number
for number in numbers
if number > 0
)
print(result)Output:
60The positive values are:
10 + 20 + 30 = 609. Sum Negative Numbers
numbers = [10, -5, 20, -3, 30]
result = sum(
number
for number in numbers
if number < 0
)
print(result)Output:
-810. sum() with filter()
Since you have already learned filter(), you can combine it with sum().
numbers = [10, 25, 40, 15, 30, 5]
result = sum(
filter(
lambda x: x >= 20,
numbers
)
)
print(result)Output:
95First filter() selects:
25, 40, 30Then sum() calculates:
25 + 40 + 30 = 9511. sum() with map()
You can also combine map() and sum().
numbers = [1, 2, 3, 4, 5]
result = sum(
map(
lambda x: x * 2,
numbers
)
)
print(result)Output:
30map() produces:
2, 4, 6, 8, 10Then:
2 + 4 + 6 + 8 + 10 = 3012. Sum Squares
numbers = [1, 2, 3, 4, 5]
result = sum(
x ** 2
for x in numbers
)
print(result)Output:
55Calculation:
1² + 2² + 3² + 4² + 5²
= 1 + 4 + 9 + 16 + 25
= 5513. Sum Even Numbers
numbers = [1, 2, 3, 4, 5, 6]
result = sum(
x
for x in numbers
if x % 2 == 0
)
print(result)Output:
12Because:
2 + 4 + 6 = 1214. Sum Odd Numbers
numbers = [1, 2, 3, 4, 5, 6]
result = sum(
x
for x in numbers
if x % 2 != 0
)
print(result)Output:
9Because:
1 + 3 + 5 = 915. Calculate Total Marks
marks = [75, 85, 65, 90, 80]
total = sum(marks)
print("Total:", total)Output:
Total: 39516. Calculate Average Marks
sum() can be combined with len() to calculate an average.
marks = [75, 85, 65, 90, 80]
average = sum(marks) / len(marks)
print("Average:", average)Output:
Average: 79.0The formula is:
Average = Total / Number of Values17. Sum Student Marks
students = [
("Ram", 75),
("Sita", 85),
("Hari", 65),
("Gita", 90)
]
total = sum(
student[1]
for student in students
)
print(total)Output:
31518. Average Student Marks
students = [
("Ram", 75),
("Sita", 85),
("Hari", 65),
("Gita", 90)
]
total = sum(
student[1]
for student in students
)
average = total / len(students)
print("Average:", average)Output:
Average: 78.7519. Sum Dictionary Values
students = {
"Ram": 75,
"Sita": 85,
"Hari": 65,
"Gita": 90
}
total = sum(students.values())
print(total)Output:
31520. Sum List of Dictionaries
This is especially useful when working with API or database data.
students = [
{"name": "Ram", "marks": 75},
{"name": "Sita", "marks": 85},
{"name": "Hari", "marks": 65},
{"name": "Gita", "marks": 90}
]
total = sum(
student["marks"]
for student in students
)
print(total)Output:
31521. Sum Product Prices
products = [
{"name": "Laptop", "price": 80000},
{"name": "Mouse", "price": 1500},
{"name": "Keyboard", "price": 3000}
]
total = sum(
product["price"]
for product in products
)
print(total)Output:
8450022. Sum Salaries
employees = {
"Ram": 35000,
"Sita": 45000,
"Hari": 30000,
"Gita": 55000
}
total_salary = sum(
employees.values()
)
print(total_salary)Output:
16500023. Sum After Filtering
Suppose we only want salaries above 40000.
employees = {
"Ram": 35000,
"Sita": 45000,
"Hari": 30000,
"Gita": 55000
}
result = sum(
salary
for salary in employees.values()
if salary > 40000
)
print(result)Output:
100000Because:
45000 + 55000 = 10000024. sum() with Boolean Values
In Python:
True = 1
False = 0Therefore:
values = [True, False, True, True]
print(sum(values))Output:
3This can be useful for counting how many conditions are true.
25. Count Passing Students
marks = [75, 45, 80, 32, 90, 55]
passed = sum(
mark >= 40
for mark in marks
)
print("Passed:", passed)Output:
Passed: 5Python evaluates:
True, True, True, False, True, Truewhich becomes:
1 + 1 + 1 + 0 + 1 + 1 = 526. Count Failed Students
marks = [75, 45, 80, 32, 90, 55]
failed = sum(
mark < 40
for mark in marks
)
print("Failed:", failed)Output:
Failed: 127. Sum Using range()
result = sum(range(1, 11))
print(result)Output:
55This calculates:
1 + 2 + 3 + ... + 1028. Sum Multiples of 5
numbers = range(1, 51)
result = sum(
x
for x in numbers
if x % 5 == 0
)
print(result)Output:
275The selected numbers are:
5 + 10 + 15 + 20 + 25 + 30 + 35 + 40 + 45 + 5029. sum() with zip()
You can combine two lists and calculate their combined values.
prices = [100, 200, 300]
quantities = [2, 3, 4]
total = sum(
price * quantity
for price, quantity in zip(
prices,
quantities
)
)
print(total)Output:
2000Calculation:
100 × 2 = 200
200 × 3 = 600
300 × 4 = 1200
Total = 200030. Practical Example : Shopping Cart
cart = [
{"name": "Laptop", "price": 80000, "qty": 1},
{"name": "Mouse", "price": 1500, "qty": 2},
{"name": "Keyboard", "price": 3000, "qty": 1}
]
total = sum(
item["price"] * item["qty"]
for item in cart
)
print("Total:", total)Output:
Total: 8600031. Practical Example : Monthly Expenses
expenses = [
5000,
3000,
2500,
4000,
3500
]
total = sum(expenses)
print("Total Expenses:", total)Output:
Total Expenses: 1800032. Practical Example : Revenue
sales = [
{"product": "Laptop", "revenue": 80000},
{"product": "Mouse", "revenue": 15000},
{"product": "Keyboard", "revenue": 20000}
]
total_revenue = sum(
sale["revenue"]
for sale in sales
)
print("Revenue:", total_revenue)Output:
Revenue: 11500033. Important Difference: sum() vs max() vs min()
You have now learned:
min() → Smallest value
max() → Largest value
sum() → Total value
sorted() → Sorted valuesExample:
numbers = [10, 20, 5, 30]min(numbers)5max(numbers)30sum(numbers)65sorted(numbers)[5, 10, 20, 30]34. Important Points to Remember
Basic syntax
sum(iterable)With starting value
sum(iterable, start)List
sum([10, 20, 30])Dictionary values
sum(data.values())With generator expression
sum(
x
for x in numbers
if x > 10
)With map()
sum(
map(lambda x: x * 2, numbers)
)With filter()
sum(
filter(lambda x: x > 10, numbers)
)