Methods and Functions

Python sum()

Python sum () function is a built-in function that returns the sum of all numerical values provided in an iterable. The numerical values that are passed in the function can be integer and floating-point numbers as well. 

In Python, one of the most used functions is the sum. It is used in several cases to add the items of an iterable. The sum () function simplifies the process of obtaining the total value of all the items in the iterables. The iterable can be in the form of a list containing numerical values. 

The Python sum () function is used in the syntax provided below. 

sum (iterable, start)

Sum() Parameters 

As shown in the syntax, the sum () function requires two parameters. The function adds the start with the items present in the iterable from left to right. 

The iterable refers to lists, tuplet ts, dictionaries, etc., containing numerical values like integers or floating points. 

The argument named start is an optional parameter. The value of start is added to the sum of items present in iterable. The default value of start, if not mentioned or when omitted, is 0. 

Return Value from sum()

The sum () function will return the total sum of all the elements present in the iterable that was passed as an argument and the start. 

Example: Working of Python sum()

The program provided below shows how any list or tuple can be used to obtain the sum of all the elements present in it. Note that the list should not include any character or string as an element. 

num = [1, 3, 5, 7, 9, 11]

sum1 = sum (num)

print (sum1)

sum1 = sum (num, 13)

print (sum1)

Here, the first sum () function does not include the start parameter. It is included in the second sum () function. When you print the output of both these functions, you will find that their results are different.

You can similarly add negative, positive, and floating-point numbers with the sum () function. If you want the sum of floating-point numbers to be exactly precise, you should use the function math.fsum (iterable). 

The sum () method is different from the join () function. The sum () function is used to add the numerical values, whereas the join () function concatenates the items of the iterables. These items are in the form of strings or characters. 

Frequently Asked Questions

What is SUM () in Python?

The sum () function in Python is available in the function directory that provides the sum of all the numerical values present in any iterable. The numerical values can be in the form of integers and floating-point values. 

It computes the sum of all numerical values in an iterable. A number can be added to the total using the sum() function’s optional parameter. It comes in handy when you need to know the total value of a list of things, which is a common requirement in various mathematical calculations.

How do you sum a number in Python?

To obtain the sum of numbers in Python, one can either use the addition operator, the” +” operator that adds the numerical values placed on the left and right sides of the operator. The sum () function can also be used to get the total sum of all numbers present in an iterable. 

How does SUM work in Python?

For the sum () function in Python, two parameters are required. The first is iterable, a list, tuple, or dictionary containing numerical values. Another optional argument is the sum that is a number that needs to be added to the elements of the iterable. 

How do you sum a string in Python? 

To sum a string in Python, the join () function is used. The join () function is also built-in in the Python library and provides the string formed after the concatenation of two or more strings. 

Share with friends

Customize your course in 30 seconds

Which class are you in?
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
Ashhar Firdausi
IIT Roorkee
Dr. Nazma Shaik
Gaurav Tiwari
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.