Post

Python sum()

The sum() is a built-in python function which sums all the items of a given iterable.

Python sum()

The syntax of the sum() function is:

1
2
sum(iterable, start)

sum() Parameters

The sum() functions takes two parameters as argument:

  • iterable - Any iterable or sequence which contains numbers. For Example list, tuple, dict, etc)
  • start (Optional) - A value that is added to the return value. Default it will take 0.

Let see some examples of sum() in python.

Example 1: How to use sum() with python list?

1
2
3
4
5
6
7
8
9
10
numbers = [2,8,4,3,7,1]

# start parameter is not provided
numbers_sum = sum(numbers)
print(numbers_sum)

# start = 10
numbers_sum = sum(numbers, 10)
print(numbers_sum)

Output:

1
2
3
25
35

Example 2: Using floating numbers with sum() function.

1
2
3
4
5
6
numbers = [4.6,6.2,0.4,5.3]

# start parameter is not provided
numbers_sum = sum(numbers)
print(numbers_sum)

Output:

1
2
16.5

Example 3 : Using negative numbers with sum() functions.

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

# start parameter is not provided
numbers_sum = sum(numbers)
print(numbers_sum)

Output:

1
2
-23

Rules of sum()

  • sum() function takes on iterable as parameters.
  • All the iterable must contain numbers as all items.
This post is licensed under CC BY 4.0 by the author.