Python

Python sum() Function

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() or map()
  • Calculating averages
  • Working with numerical data

1. Basic Example

numbers = [10, 20, 30, 40, 50]

result = sum(numbers)

print(result)

Output:

150

2. sum() with a Tuple

numbers = (10, 20, 30, 40)

print(sum(numbers))

Output:

100

3. sum() with a Set

numbers = {10, 20, 30, 40}

print(sum(numbers))

Output:

100

4. sum() with a Range

numbers = range(1, 6)

print(sum(numbers))

Output:

15

The values are:

1 + 2 + 3 + 4 + 5 = 15

5. 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:

160

Python calculates:

100 + 10 + 20 + 30 = 160

7. Default start Value

If start is not provided, Python uses 0.

numbers = [10, 20, 30]

print(sum(numbers))

Internally:

0 + 10 + 20 + 30

Result:

60

8. Sum Positive Numbers

numbers = [10, -5, 20, -3, 30]

result = sum(
    number
    for number in numbers
    if number > 0
)

print(result)

Output:

60

The positive values are:

10 + 20 + 30 = 60

9. Sum Negative Numbers

numbers = [10, -5, 20, -3, 30]

result = sum(
    number
    for number in numbers
    if number < 0
)

print(result)

Output:

-8

10. 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:

95

First filter() selects:

25, 40, 30

Then sum() calculates:

25 + 40 + 30 = 95

11. 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:

30

map() produces:

2, 4, 6, 8, 10

Then:

2 + 4 + 6 + 8 + 10 = 30

12. Sum Squares

numbers = [1, 2, 3, 4, 5]

result = sum(
    x ** 2
    for x in numbers
)

print(result)

Output:

55

Calculation:

1² + 2² + 3² + 4² + 5²
= 1 + 4 + 9 + 16 + 25
= 55

13. Sum Even Numbers

numbers = [1, 2, 3, 4, 5, 6]

result = sum(
    x
    for x in numbers
    if x % 2 == 0
)

print(result)

Output:

12

Because:

2 + 4 + 6 = 12

14. Sum Odd Numbers

numbers = [1, 2, 3, 4, 5, 6]

result = sum(
    x
    for x in numbers
    if x % 2 != 0
)

print(result)

Output:

9

Because:

1 + 3 + 5 = 9

15. Calculate Total Marks

marks = [75, 85, 65, 90, 80]

total = sum(marks)

print("Total:", total)

Output:

Total: 395

16. 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.0

The formula is:

Average = Total / Number of Values

17. Sum Student Marks

students = [
    ("Ram", 75),
    ("Sita", 85),
    ("Hari", 65),
    ("Gita", 90)
]

total = sum(
    student[1]
    for student in students
)

print(total)

Output:

315

18. 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.75

19. Sum Dictionary Values

students = {
    "Ram": 75,
    "Sita": 85,
    "Hari": 65,
    "Gita": 90
}

total = sum(students.values())

print(total)

Output:

315

20. 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:

315

21. 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:

84500

22. Sum Salaries

employees = {
    "Ram": 35000,
    "Sita": 45000,
    "Hari": 30000,
    "Gita": 55000
}

total_salary = sum(
    employees.values()
)

print(total_salary)

Output:

165000

23. 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:

100000

Because:

45000 + 55000 = 100000

24. sum() with Boolean Values

In Python:

True  = 1
False = 0

Therefore:

values = [True, False, True, True]

print(sum(values))

Output:

3

This 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: 5

Python evaluates:

True, True, True, False, True, True

which becomes:

1 + 1 + 1 + 0 + 1 + 1 = 5

26. Count Failed Students

marks = [75, 45, 80, 32, 90, 55]

failed = sum(
    mark < 40
    for mark in marks
)

print("Failed:", failed)

Output:

Failed: 1

27. Sum Using range()

result = sum(range(1, 11))

print(result)

Output:

55

This calculates:

1 + 2 + 3 + ... + 10

28. Sum Multiples of 5

numbers = range(1, 51)

result = sum(
    x
    for x in numbers
    if x % 5 == 0
)

print(result)

Output:

275

The selected numbers are:

5 + 10 + 15 + 20 + 25 + 30 + 35 + 40 + 45 + 50

29. 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:

2000

Calculation:

100 × 2 = 200
200 × 3 = 600
300 × 4 = 1200

Total = 2000

30. 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: 86000

31. Practical Example : Monthly Expenses

expenses = [
    5000,
    3000,
    2500,
    4000,
    3500
]

total = sum(expenses)

print("Total Expenses:", total)

Output:

Total Expenses: 18000

32. 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: 115000

33. Important Difference: sum() vs max() vs min()

You have now learned:

min()     → Smallest value
max()     → Largest value
sum()     → Total value
sorted()  → Sorted values

Example:

numbers = [10, 20, 5, 30]
min(numbers)
5
max(numbers)
30
sum(numbers)
65
sorted(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)
)
Interactive Sandbox
Python