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.
sum(iterable, start)
iterable A sequence of numbers such as a list, tuple, or set whose values will be added.
start (optional) A value that is added to the total. The default value is 0.
numbers = [10, 20, 30, 40]
result = sum(numbers)
print(result)
Output
100
Explanation
numbers = (5, 10, 15)
print(sum(numbers))
Output
30
Explanation
start Parameternumbers = [1, 2, 3]
print(sum(numbers, 10))
Output
16
Explanation
numbers = {2, 4, 6, 8}
print(sum(numbers))
Output
20
Explanation
sum() function works with any iterable containing numeric values.marks = [85, 90, 78, 88]
total = sum(marks)
print("Total Marks:", total)
Output
Total Marks: 341
Explanation
sum() works only with numeric values.sum() is a built-in Python function used to calculate the total of numeric elements in an iterable.