Menu Close

Python sum Function

python sum function

In this article, we are going to learn all about the Python sum function to calculate the sum of all the items in iterable. In the previous tutorial, we have seen lots of Python built-in functions along with examples. The sum() function is a built-in function that comes with Python by default, you don’t need to use any package to use of sum() function.

sum function in Python

Python sum is a built-in function that comes with Python default. Python sum function is used to return the sum of all the items in an iterable. Iterable can be a tuple, list, and set.

Syntax:

The syntax of the sum function in Python is:-

sum(iterable, start)

Parameter:

sum function accepts two parameters.

  • iterable:- Required, The sequence to calculate the sum.
  • start: Optional, A value that is added to return value.

Return Value:

The return value of the Python sum function is added value.

Python sum function example:

Here we will take some examples of the sum function so that you don’t have any confusion regarding the sum function in Python.

Example: Adding all the items to the list


lst = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print("List items are:- ", *lst)
print("Sum of all the items of the list is:- ", sum(lst))

Output

List items are:-  1 2 3 4 5 6 7 8 9
Sum of all the items of the list is:-  45

Example: Adding all the items to the set


set = {10, 20, 30, 40, 50}
print("Set items are:- ", *set)
print("Sum of all the items of the set is:- ", sum(set))

Output

Set items are:-  50 20 40 10 30
Sum of all the items of the set is:-  150

Example: Adding all the items to the tuple


tuple = {10, 20, 30, 40, 50}
print("Tuple's items are:- ", *tuple)
print("Sum of all the items of the tuple is:- ", sum(tuple))

Output

Tuple's items are:-  50 20 40 10 30
Sum of all the items of the tuple is:-  150

Example: Python sum function with the second parameter

The second parameter of the Python sum() function will add to the final sum of the iterable’s items.


lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
result = sum(lst, 45)
print(result)

The output will be:- 100

As you can see in the above example, the total sum of the iterable’s items is 55, and 45 is added to 55, The final output will be 100.

Example: Passing iterable to sum function having string items

If you will pass an iterable having string items to the sum function, the sum function will raise an Exception TypeError.


lst = ["Programming", "Funda"]
result = sum(lst)
print(result)

Output

Traceback (most recent call last):
  File "C:\Users\Vishvajit Rao\OneDrive\Desktop\Pyspark\main.py", line 75, in <module>
    result = sum(lst)
TypeError: unsupported operand type(s) for +: 'int' and 'str'

Conclusion

So, In this article, you have learned all about the Sum function in Python along with examples. Python sum is the best function to calculate items of iterable. Iterable can be anything from a list, set, and tuple having integer items. Python sum function will raise an exception TypeError If you pass an iterable that contains string items. If this article is helpful for you, please share and keep visiting for further python tutorials.

For more information:- Click Here

Thanks for your valuable time….🧑‍💻🧑‍💻❤️❤️

Python tuple() Function
Python super() function

Related Posts