What is sum (a) in python?

The sum() function adds the items of an iterable and returns the sum.

Example

marks = [65, 71, 68, 74, 61]

# find sum of all marks total_marks = sum(marks)

print(total_marks) # Output: 339


sum() Syntax

The syntax of the sum() function is:

sum(iterable, start)

The sum() function adds start and items of the given iterable from left to right.


sum() Parameters

  • iterable - iterable (list, tuple, dict, etc). The items of the iterable should be numbers.
  • start (optional) - this value is added to the sum of items of the iterable. The default value of start is 0 (if omitted)

sum() Return Value

sum() returns the sum of start and items of the given iterable.


Example: Working of Python sum()

numbers = [2.5, 3, 4, -5]

# start parameter is not provided

numbers_sum = sum(numbers)

print(numbers_sum) # start = 10

numbers_sum = sum(numbers, 10)

print(numbers_sum)

Output

4.5
14.5

If you need to add floating-point numbers with exact precision, then you should use math.fsum(iterable) instead.

If you need to concatenate items of the given iterable (items must be strings), then you can use the join() method.

'string'.join(sequence)

Visit this page to learn about, Python join() Method

The Python sum() function calculates the total of all numerical values in an iterable. sum() works with both integers and floating-point numbers. The sum() function has an optional parameter to add a number to the total.


alculating the sum of a list is a common operation in Python. For example, let’s say you are a coffee shop owner who wants to know the total value of all sales made last month. You could use the sum() function to perform this calculation.

What is sum (a) in python?

Find Your Bootcamp Match

  • Career Karma matches you with top tech bootcamps
  • Access exclusive scholarships and prep courses

Select your interest
First name

Last name

Email

Phone number

By continuing you agree to our Terms of Service and Privacy Policy, and you consent to receive offers and opportunities from Career Karma by telephone, text message, and email.

In Python code, the sum() function can be used to calculate the sum of all values in an iterable object. This method is useful when you need the total value of a list of items, which is common in a number of mathematical calculations.

In this tutorial, we are going to discuss how to use the Python sum() method. We’ll go through a few examples to showcase how this method works in a real program.

Python Sum Syntax

The Python sum() function adds up all the numerical values in an iterable, such as a list, and returns the total of those values. sum() calculates the total of both floating-point numbers and integers.

For instance, you could use the sum() method to calculate the total price of the products a customer purchases at a store.

Here’s the syntax for the Python sum() method:

sum(iterable_object, start_value)

The function sum() takes in two parameters:

  • The iterable object that you would like to calculate the total of (required)
  • An extra number that you want to add to the value you’re calculating (optional)

Let’s use an example to illustrate how the Python sum() function works.

Sum Python Example

Let’s say that we operate a local store and want to calculate the total amount to charge a customer for their shopping. We already have a list containing the prices of each individual product, but now we want to get the total value of that list.

We could use the sum() function for this purpose. Here’s an example of the sum() function being used to calculate the total cost of a customer’s shopping:

products_purchased = [5.40, 2.20, 9.95, 1.50, 1.50, 2.20, 4.65, 3.00, 2.00]

total_price = sum(products_purchased)
print(total_price)

Our program returns the sum: 32.40. Let’s break down our code and discuss how it works.

On the first line, we declare a Python variable called products_purchased which stores the prices of each product a customer has purchased.

On the next line, we calculate the total price of the customer’s shop by using the sum() function on our products_purchased Python array. Next, our program prints out the total price of the customer’s shop that has been calculated using sum().

Python Sum Function: Using a Tuple

In the above example, we have used sum() to calculate the total value of several items in a list. But if our values were stored in a Python tuple, we could also have used sum(). Here’s an example of sum() being used to calculate the total value of a tuple:

products_purchased = (5.40, 2.20, 9.95, 1.50, 1.50, 2.20, 4.65, 3.00, 2.00)

total_price = sum(products_purchased)
print(total_price)

Our code returns: 32.40. This program is almost exactly the same as the one above. The only difference being that our products_purchased variable has been assigned a tuple rather than a list.

sum in Python Second Argument

The sum() function takes in an optional second parameter which adds a number to the final total calculated by the method.

Let’s say that a customer has decided to purchase a bag after their groceries have been scanned. Each bag costs $1. To add the $1 bag to the customer’s total, you could specify a second parameter in the sum() method. 

Here’s the code that we could use to calculate the price of a customer’s purchase, in addition to the $1 bag they have bought:

products_purchased = [2.00, 3.00, 4.00]

total_price = sum(products_purchased, 1.00)
print(total_price)

Our program returns: 10.00. When our final value 1 is added to the sum() method, it is added and so our program returns 10.00.

Conclusion

The sum() method can be used in Python to calculate the total of the items stored in an iterable object. For example, sum() could be used to calculate the cost of your coffee shop order.

In this tutorial, we explored how to use the sum() function in Python. Then, we discussed how to use the second parameter offered by sum() to add more values to the total calculation. Now you’re equipped with the knowledge you need to use the sum() method in Python like a pro!

To learn more about coding in Python, read our How to Learn Python guide.

Is sum a method in Python?

The sum() method can be used in Python to calculate the total of the items stored in an iterable object.

Why is sum a keyword in Python?

sum() function in Python Python provides an inbuilt function sum() which sums up the numbers in the list. Syntax: sum(iterable, start) iterable : iterable can be anything list , tuples or dictionaries , but most importantly it should be numbers. start : this start is added to the sum of numbers in the iterable.

How do you write sum in Python?

The sum() function is used to get the sum of all items in an iterable..
Version: ... .
Syntax: sum(iterable[, start]).
Parameter: ... .
Return value: ... .
Example: Python sum() num = [3.5, 5, 2, -5] # start parameter is not provided numSum = sum(num) print(numSum) # start = 15 numSum = sum(num, 15) print(numSum) ... .
Pictorial Presentation:.

Can you sum a list in Python?

Python's built-in function sum() is an efficient and Pythonic way to sum a list of numeric values.