There are many ways to sum a list in python. But in this tutorial, you will learn how to sum a list in Python using a for loop.
Steps to sum a list in Python using a for loop
Lets know all the steps to sum a list in python using for loop.
Step 1: Create a list of numbers
First, let’s create a list of numbers we will be using the for loop on them to add together. We can use a for loop to iterate over each element in the list and calculate the sum in a variable.
sample_list = [10, 20, 30]
Step 2: Create a temp variable to store the sum
In this step, you will create a temporary variable that will store the sum of all the elements of the list.
total_sum = 0
Step 3: Use the for loop
Now the next step is to iterate each element of the loop and with each iteration add the element with the total_sum variable.
for element in sample_list:
total_sum += element
Step 4: Print the sum
The last step is to print the sum of the elements. For this, I will use the print() function.
print("The sum of the list is:", total_sum)
Run the below lines of code.
sample_list = [10, 20, 30]
total_sum = 0
for element in sample_list:
total_sum += element
print("The sum of the list is:", total_sum)
Output

Explanation of the code
In this code, you have a list of numbers called sample_list. Then you will initialize a variable called total_sum to zero. After that, you will use a for loop to iterate over each element in the list. For each element, you will add it to the total_sum variable using the += operator, which is a shorthand for total_sum = total_sum + element.
After the loop completes, total_sum will contain the sum of all elements in the list. Finally, you will print the result using the print() function.
Let’s try all the steps of the second list.
sample_list = [100, 200, 300,400]
total_sum = 0
for element in sample_list:
total_sum += element
print("The sum of the list is:", total_sum)
Output

In this case, you have a list of numbers [100, 200, 300,400], and using the for loop you will print the total sum of all the elements of the list.
Conclusion
That’s all the steps on how you can sum a list in Python using a for a loop. It’s a simple and effective way to perform sum operations on any list of numbers.
I hope you found this tutorial helpful.
Leave a Reply