What’s New ?

The Top 10 favtutor Features You Might Have Overlooked

Read More

Sum in Python: How to use sum() function? with Examples

  • Nov 25, 2022
  • 5 Minute Read
  • Why Trust Us
    We uphold a strict editorial policy that emphasizes factual accuracy, relevance, and impartiality. Our content is crafted by top technical writers with deep knowledge in the fields of computer science and data science, ensuring each piece is meticulously reviewed by a team of seasoned editors to guarantee compliance with the highest standards in educational content creation and publishing.
  • By Abrar Ahmed
Sum in Python: How to use sum() function? with Examples

When we are coding, there are numerous occasions on which we have to find the sum of certain numbers. But it is a tedious task to define the function to calculate the sum of an iterable over and over again.

In python, there is an easier way out than defining the function for calculating the sum over and over again. This built-in function in python to calculate the sum of any given iterable is called the sum function.

But first, we must revise different iterables in this programming language, so to better understand how to use the sum() function in each case.

What are Iterables?

Iterables in python are defined as any collection of items in any of the four built-in data structures that python offers. These four data structures that python offers are:

01) Lists

A list is defined as a collection of items. Lists are defined in python using the list() function. Alternatively, you could go ahead and put all the items that you want in a list, inside square braces and that will create a list for you. Lists can include all types of items and are heterogeneous.

02) Tuples

Tuples are also a collection of items, they are also created in python using the tuple() function. Alternatively, you can take all your items and put them in a comma-separated list between two round braces and that becomes your tuples. Tuples can include all types of items too but they are not mutable. That is you cannot make a change once you define the tuple.

03) Dictionary

Dictionaries are a collection of key-element pairs. They can be created by defining key-value pairs. They are defined using the dictionary function and can be created in the following manner: {key1:value1, key2:value2; etc }. Dictionaries are heterogenous and can be mutable too.

04) Sets

Sets are again a collection of items too, and they are very similar to lists. They are also declared using the set() function or by placing all the items inside curly braces. But the difference between sets and lists is that only unique items will be present inside sets.

#list
lis1 = [25,15,14]

#tuple
tup1 = (25,15,14)

#dictionary
dict1 = {"key1":5 , "key2":6 , "key3":7}

#set
set1 = {25,15,14}

 


What is the Sum function?

Now that we know all the tables that are possible in python, let us dive into what the sum function is and how we can use it. The sum function is used to find the sum of all the items in the given iterable. But we knew that already.

So how do we use the sum function? The syntax for the sum function is sum(iterable, start).

Here, iterable can be a list, tuple, or dictionary & start is an optional value that is later added to the sum value of the iterable. 

Let us look at a few examples to understand this concept.

Use Cases of the Sum function

The sum function can be used to find the sum of any iterables but there is one limitation to the sum function. The sum function will throw an error if it is given anything but numbers. What is an error? Well, an error is a fatal mistake that will make the code stops running and we should always avoid that. Sometimes our lists may contain a mix of letters and numbers but if apply a sum function to such a list we will end up with an error that will not allow the program to run further.

Sum in a list

#list
lis1 = [25,15,14]

print(sum(lis1))

 

Output:

54

 

Sum in a tuple

#tuple
tup1 = (25,15,14)

print(sum(tup1))

 

Output:

54

 

Sum in a set

#set
set1 = {25,15,14}

print(sum(set1))

 

Output:

54

 

Sum in a dictionary

#dictionary
dict1 = {"key1":1, "key2":2, "key3":30}

print(sum(dict1.values()))

 

Output:

33

 

Sum for adding binary numbers

num1 = input("Enter the first binary number\n")
num2 = input("Enter the second binary number\n")
num1 = int(num1,2)
num2 = int(num2,2)
total = sum([num1,num2])
print(bin(total))

 

Output:

Enter the first binary number
10
Enter the second binary number
11
0b101

 

Conclusion

A sum function is a powerful tool in python that can be used to find the total of all the items in a given iterable. There is only one error that can occur when using the sum function and that is the iterable should not contain anything else but numbers. These numbers can be whole numbers or decimal point numbers.

FavTutor - 24x7 Live Coding Help from Expert Tutors!

About The Author
Abrar Ahmed
An ambivert individual with a thirst for knowledge and passion to achieve. Striving to connect Artificial Intelligence in all aspects of life. I am also an avid coder and partake in coding challenges all the time on Leetcode and CodeChef.