Python

Python Sum List

When two lists are combined, a new list is created with each element equal to the sum of the items in the two lists’ respective positions. Therefore, the sum of the integers in the list is needed. The sum() method in Python computes the sum of all mathematical values in an iterable. Either integers and floating-point values can be added with sum(). A number can be added to the total using the sum() function’s additional parameter.

In Python, computing the sum of a list is a basic procedure. This technique is handy when you want to know the whole value of a list of things, which is required in many mathematical formulas.

We will examine how to apply and implement the Python sum() function in this tutorial. We will go over a few instances to determine how this approach works in practice. Let’s create any file in your Sypder tool. It will have a “.py” extension.

Pre-requisites

  • Windows 10
  • Installed Spyder Tool

Example 1

In this example, we are going to use the sum () function in Python. For this illustration, we have used a list titled “values”. We have included a few integer values in it. After that, there is a variable “s”. It will keep the sum value. You will observe that we have not declared any start parameter. If we will not specify any start value, you may consider it as “0”.

Then we have a print statement that will show the end result on the output screen. In the second last line of a code, the start parameter is specified as “10”. You will get two different results, one will correspond to a start value specified in the code, the other will assume it as “0”. Now, copy the code that is written here.

  • values = [10,21,31,42,52,12,42,52]
  • s = sum(values)
  • print(s)
  • s = sum(values, 10)
  • print(s)

As soon as you save the file and hit the “Run” button of your tool, you will get the expected result that can be checked from the attached image as well.

Example 2

Now, we have another simple example for you to work on.  We will calculate the total marks here. We have used a list titled “marks”. We have provided a few integer values in it. They are specified as marks. After that, a variable “total_m” will keep the value of the sum of total marks. You will observe that we have not stated any start parameter. If we will not specify any start value, you may consider it as “0”. Then, we have a print statement that will exhibit the end result on the output screen. Copy the code that is written here.

  • marks = [95, 91, 88, 54, 41]
  • total_m = sum(marks)
  • print(total_m)

As soon as you save the created file and hit the “Run” button of your tool, you will get the expected result that can be verified from the affixed image as well.

Example 3

This example will demonstrate where we will calculate the sum to get the average of the numbers. We have used a list titled “num”. We have provided a few integer values in it. They are specified as numbers in the list. After that, a variable “s” will keep the value of the sum of total numbers in the list. You can confirm that we have not provided any start parameter.

If we will not specify any start value, you may consider it as “0”. After that we have another variable titled “avg” that will calculate the average of the numbers, then we have a print statement that will demonstrate the end result on the output screen. Now, copy the code that is written here.

  • num = [1,24,34,46,58,10,46,5]
  • s = sum(num)
  • avg= s/len(num) 
  • print (avg)

Once you save the created file and hit the “Run” button of your tool, you will get the predictable result that can be confirmed from the affixed image as well.

Conclusion

This article is all about the usage of the sum function in Python. We have elaborated on three different examples. Some of them computed the result with start parameters as well. Now, we expect you will get help while you use the sum function during your work.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content