array Python module
| ast Python module
| code Python module
| COM PHP module
| dis Python module
| Ev PHP module
| io Python module
| math Python module
| numbers Python module
| os Python module
| Python functions
| re Python module
| UI PHP module
👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!
The Python sum () function calculates the total of all numeric 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.
Computing the sum of a list is a common operation in Python. For example, suppose you are the owner of a cafe and want to know the total value of all sales made in the past month. You can 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 many math calculations.
In this tutorial, we’ll see how to use the Python sum () method. We will look at some examples to show how this method works in a real program.
Python Sum Syntax
The Python sum () function sums all the numeric values ‚Äã‚Äãin an iterable, such as a list, and returns the total of those values. sum () calculates the total of floating point numbers and integers.
For example, you can use the sum () method to calculate the total price of products for a customer store in a store.
Here is the syntax of the Python sum () method:
The sum () function accepts two parameters:
- The iterable object you want to total (required)
- An additional number you want to add to the value you are calculating (optional)
Let’s use an example to illustrate how Python’s sum () function works.
Example of a Python sum
Suppose we are running a local store and we wanted to calculate the total amount to charge a customer for their purchases. price of each individual produced, but now we want to get the total value of this listing.
We could use the sum () function for this purpose. Here is an example of the sum () used to calculate the total cost of a customer’s purchases:
Our program returns the sum: 32.40. Let’s review our code and discuss how it works.
In the per-line prim , we declare a Python variable called products_purchased which stores the prices of each product purchased by a customer.
On the next line, calculate the customer’s total store price using the sum () function on our products_purchased Python array . Then our program prints the total price of the customer’s store which was calculated using sum ().
Python sum function: using a tuple
In the example above, we used sum () to calculate the total value of several elements of a list. But if our values ‚Äã‚Äãwere stored in a Python tuple , we could also have used sum (). Here is an example of sum () used to calculate the total value of a tuple:
Our code returns: 32.40. This program is almost exactly the same as the one above. The only difference is that our products_purchased variable has been given a tuple instead of a list.
sum as second Python argument
The sum () accepts an optional second parameter that adds a number to the final total calculated by the method.
Suppose a customer decides to buy a bag after their groceries have been scanned. Each bag costs $ 1 . To add the $ 1 bag to the customer’s total, you may need to specify a second parameter in the sum () method.
Here is the code we could use to calculate a customer’s purchase price, plus the $ 1 bag they bought:
Our program returns: 10.00. When our final value 1 is added to sum (), it is added and then our program returns 10.00.
The sum () method can be used in Python to calculate the total of elements stored in an iterable object. For example, sum () could be used to calculate the cost of your bar order.
In this tutorial, we explored how to use sum () function in Python. So we discussed how to use the second parameter offered by sum () to add more values ‚Äã‚Äãto the total calculation. You now have the knowledge to use the sum () method in Python like a pro!
To learn more about programming in Python, read our Python Learning Guide .
👻 Read also: what is the best laptop for engineering students?
Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.
By the way, this material is also available in other languages:
Abu Dhabi | 2023-02-02
Rome | 2023-02-02
Rome | 2023-02-02