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.
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:
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.
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.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.