sum() function is used to get the sum of numbers in a list, tuple, or dictionary. The
sum() function adds integers and floating point numbers to get a total from an iterable object. The function also has an optional parameter to add an additional amount to the total.
When you’re working with a list of values in Python, there may be a case where you want to get the sum of every value in the list. 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.
In Python code, the
sum() function can be used to calculate the sum of all values in an iterable object, such as a list or a set. 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 to calculate the sum of all items within a particular iterable object. We’ll also go through a few examples to showcase how this method works in a real program.
sum() function is used to calculate sums of compound data types, which include tuples, dictionaries, and lists. For instance, the
sum() method could be used to calculate the total price of the products a customer purchases at a store or to get the total value of beverages purchased at a coffee shop in a day. It can be used to add floating-point numbers as well as integers in an iterable object.
sum() takes in two parameters: the iterable object that you would like to calculate the total of, and an extra number that you want to add to the value you’re calculating. Here’s the syntax for the Python
Let’s use an example to illustrate how the Python
sum() function works. 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 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 array. Next, our program prints out the total price of the customer’s shop that has been calculated using
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 tuple or a dictionary, 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 exactly the same as the one above, with the only difference being that our
products_purchased variable has been assigned a tuple rather than a list.
sum() Second Argument
sum() function also takes in an optional second parameter which adds an additional 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
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. While the items in our array all add up to 9.00, when our final value
1 is added to the
sum() method, it is added and so our program returns 10.00.
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 or the total number of cars produced by a car manufacturer in a year.
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!