Notes

`sum()` Function in Python [ English ]

< Prev Next >

1. Introduction

The sum() function in Python is a built-in function used to calculate the total of all elements in an iterable object such as a list, tuple, or set. It adds all the numeric values together and returns the sum of those values.

The function is commonly used in programs where total values need to be calculated, such as totals of marks, prices, or numbers in a dataset.

2. Syntax

sum(iterable, start)

Parameters

  1. iterable A sequence of numbers such as a list, tuple, or set whose values will be added.

  2. start (optional) A value that is added to the total. The default value is 0.

3. Return Value


4. Basic Example

numbers = [10, 20, 30, 40]

result = sum(numbers)

print(result)

Output

100

Explanation

5. Example with Tuple

numbers = (5, 10, 15)

print(sum(numbers))

Output

30

Explanation

6. Using the start Parameter

numbers = [1, 2, 3]

print(sum(numbers, 10))

Output

16

Explanation

7. Example with a Set

numbers = {2, 4, 6, 8}

print(sum(numbers))

Output

20

Explanation

8. Practical Example

marks = [85, 90, 78, 88]

total = sum(marks)

print("Total Marks:", total)

Output

Total Marks: 341

Explanation

9. Important Points

10. Summary

< Prev Next >