# Python’s sum() Function

In Python, the sum() function is a powerful tool for calculating the sum of elements in iterables. This article aims to elucidate its usage, applications, and significance through comprehensive examples.

Understanding sum() Function

The sum() function in Python is utilized to calculate the sum of elements in iterables such as lists, tuples, or sets. Its syntax is straightforward:

sum(iterable, start=0)


Here, iterable represents the iterable object whose elements’ sum is to be calculated, and start denotes the optional initial value for the sum (default is 0).

Example 1: Sum of Numbers in a List

numbers = [1, 2, 3, 4, 5]
result = sum(numbers)
print("Sum of numbers:", result)


Output 1:

Sum of numbers: 15


Example 2: Sum of Numbers in a Tuple

numbers_tuple = (10, 20, 30, 40, 50)
result = sum(numbers_tuple)
print("Sum of numbers in tuple:", result)


Output 2:

Sum of numbers in tuple: 150


Example 3: Sum of Floating Point Numbers

float_numbers = [1.5, 2.5, 3.5, 4.5, 5.5]
result = sum(float_numbers)
print("Sum of floating point numbers:", result)


Output 3:

Sum of floating point numbers: 17.5


Example 4: Sum with Custom Start Value

numbers = [1, 2, 3, 4, 5]
start_value = 10
result = sum(numbers, start_value)
print("Sum of numbers with start value 10:", result)


Output 4:

Sum of numbers with start value 10: 25


Points to Remember

• The sum() function calculates the sum of elements in iterables.
• It can handle both numerical and non-numerical elements in the iterable.
• The start parameter allows customization of the initial value for the sum.
Author: user