It seems that you are wanting the sum of squares of the elements of ds, although your code is trying to index ds using one of its values (i), and is also trying to compute the sum inside the loop.

You can do it in one line using:

print(sum([i*i for i in ds]))

or

print(sum(i*i for i in ds))

(These two similar looking expressions do different things under the hood, although I'll skip a discussion of this for now.)

Or you could build up the sum e.g. by doing:

total = 0
for i in ds:
    total += i * i
print(total)

or use a combination of the two approaches:

squares = []
for i in ds:
    squares.append(i * i)
print(sum(squares))
Answer from alani on Stack Overflow
🌐
University of Vermont
uvm.edu › ~cbcafier › cs1210 › book › 11_loops › loops_and_summation.html
Math and Python: loops and summation – Clayton Cafiero
We begin with a tuple of numeric values, t. Since the elements of t are all numeric, we can calculate their sum. First, we create a variable to hold the result of the sum. We call this, sum_.1 Then, we iterate over all the elements in t, and at each iteration of the loop, we add the value of ...
🌐
Reddit
reddit.com › r/learnpython › how can i find the sum of the numbers in a list for a 'for' loop?
r/learnpython on Reddit: How can I find the sum of the numbers in a list for a 'for' loop?
June 1, 2024 -

I'm a complete beginner. I've been following a course through Solo Learn, and it's been okay, but the latest lesson was really poorly explained. (It wasn't explained at all. I had to rely on comments in the lesson to actually learn things. Really rough.)

The lesson is ending in a practice exercise with the following directions:

Given a list of numbers, calculate their sum using a for loop.

There's just one problem! They haven't taught us how to add anything in a for loop. After some digging on Google and Stack Overflow, I learned a few things, but I still haven't gotten the correct answer. I keep just getting wrong answers.

This is what my current code looks like. I'm at a loss and don't know what to do at this point. The list is provided by the course and I can't change it. Other than the first line, I wrote everything else.

The expected outcome is: 9803

list = [42, 8, 7, 1, 0, 124, 8897, 555, 3, 67, 99]
sum = 0

for x in list:
    sum = sum + x
    print(sum)

The outcome to this problem with my current code is: 42 50 57 58 58 182 9079 9634 9637 9704 9803

I have no idea why this happens. I just need that bottom number, but I don't know how to get it.

I originally tried doing something like

for x in list:
    sum += x
    print(sum)

But that didn't work either.

Any guidance would be really appreciated. I'm sorry if this is a simple solution, but they didn't teach us how to do this at all. Instead, they focused on modulus operations (%) for the lesson. That was really poorly explained too.

I found a similar question on Stack Overflow here. But it wasn't very helpful, and the answers use things that I haven't learned yet.

I don't know. Maybe I'm not smart. I'm not sure what to do now.

Any help is appreciated, thank you.

Discussions

How can i sum the value in list with for loop in python? - Stack Overflow
Find centralized, trusted content ... you use most. Learn more about Collectives ... Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... It seems that you are wanting the sum of squares of the elements of ds, although your code is trying to index ds using one of its values (i), and is also trying to compute the sum inside the loop... More on stackoverflow.com
🌐 stackoverflow.com
python - Get sum of numbers using a Function and For Loop - Stack Overflow
Communities for your favorite technologies. Explore all Collectives · Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on stackoverflow.com
🌐 stackoverflow.com
Can someone explain to me how to add up all the numbers in a list without using sum?
Use a for loop! Iterate over the list and add each number to the final total More on reddit.com
🌐 r/learnpython
39
21
June 27, 2022
How to cumulative sum over for loop?
First, don't overwrite list and sum, those are already existing functions. Second I am trying to take 1, and then add 4 Not exactly. You don't take 1 and then add 4. You take 0 and then add 1, then add 4, etc. So what you've forgotten is to "initialize" your sum as 0. Then you iteratively add all the values to that. l = [1, 4, 6, 3, 7, 5, 2, 8, 9] cumsum = 0 for i in l: cumsum += i If you don't first initialize a variable to 0, you have nothing to add that first value to. That's the piece you were missing. Edit: Minor improvement for clarity. More on reddit.com
🌐 r/learnpython
20
12
December 17, 2021
🌐
Bobby Hadz
bobbyhadz.com › blog › python-sum-in-for-loop
How to sum in a For or a While Loop in Python | bobbyhadz
The range class is commonly used for looping a specific number of times in for loops and takes the following parameters: If you need to sum numbers taken from user input in a for loop, use the input() function.
🌐
EyeHunts
tutorial.eyehunts.com › home › sum of n numbers in python using for loop | example code
Sum of n numbers in Python using for loop | Example code
October 4, 2023 - We use a for loop to iterate from 1 to n, adding each number to the sum_of_numbers variable. Finally, we print out the sum. You can run this code by copying and pasting it into a Python interpreter or a script file, and it will calculate the ...
🌐
Real Python
realpython.com › python-sum-function
Python's sum(): The Pythonic Way to Sum Values – Real Python
March 18, 2026 - In the second for loop, the function iterates over each item in sublist to finally populate the new flat list with .append(). Arguably, readability can be an advantage of this solution. As you’ve already learned, sum() is helpful for working with numeric values in general. However, when it comes to working with floating-point numbers, Python provides an alternative tool.
Find elsewhere
🌐
Quora
quora.com › What-is-a-Python-program-to-find-the-sum-of-numbers-between-1-and-20-using-a-for-loop
What is a Python program to find the sum of numbers between 1 and 20 using a for loop? - Quora
Answer (1 of 5): Well, you can sum an arithmetic sequence just by multiplying first+last by length/2: [code]total = (1+20) * (20//2) [/code]But that doesn’t use a for loop anywhere. You can add a useless one: [code]for _ in (): pass total = (1+20) * (20//2) for _ in range(1): total = (1+20) ...
🌐
Great Learning
mygreatlearning.com › blog › python exercises › python loop program to sum numbers
Python Loop Program to Sum Numbers
December 17, 2025 - # 1. Initialize the accumulator total_sum = 0 # 2. Loop from 1 to 100 (range stops before 101) for i in range(1, 101): total_sum = total_sum + i # 3. Print the result outside the loop print(f"The sum of numbers from 1 to 100 is: {total_sum}") ...
🌐
Python Guides
pythonguides.com › sum-elements-in-list-in-python-using-for-loop
How to Sum Elements in a List in Python
November 10, 2025 - Multiple methods exist to sum elements in a list in Python, such as for loop, sum(), list comprehension, and reduce() methods. Let’s see each technique one by one. The most obvious way to add numbers stored in a list is to use a for-loop. The following code fragment visits each list element in order and totals the sum of its elements.
🌐
Tutorial Gateway
tutorialgateway.org › python-program-to-find-sum-of-n-natural-numbers
Python Program to find Sum of N Natural Numbers
January 24, 2025 - To achieve the same, we need a loop to iterate the numbers from 1 to N and then the arithmetic operator + to add those items to the total. This program allows users to enter any integer value.
🌐
GeeksforGeeks
geeksforgeeks.org › python › sum-function-python
sum() function in Python - GeeksforGeeks
November 27, 2025 - A for loop can be used to add elements of a list one by one. This method shows how summation works step-by-step and is useful when you need full control over how each value is processed during the calculation.
🌐
PREP INSTA
prepinsta.com › home › python program › python program to find the sum of first n natural numbers
Python Program to Find the Sum of First N Natural Numbers | PrepInsta
September 27, 2022 - In this method we’ll add all the natural numbers until the given integer input using for loop in Python. num = 5 sum = 0 for i in range(num+1): sum+=i print(sum)
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › sum of n natural numbers in python
Sum of n Natural Numbers in Python | Logic and Examples
June 20, 2026 - One of the most common ways is ... user sum_n = 0 # Initialize the sum variable # Loop through the first n natural numbers and add them to sum_n for i in range(1, n + 1): # range(1, n+1) includes numbers from 1 to n sum_n ...
🌐
Reddit
reddit.com › r/learnpython › how to cumulative sum over for loop?
r/learnpython on Reddit: How to cumulative sum over for loop?
December 17, 2021 -

Hello, so I am trying to understand how to sum values through a for loop within python so that I start with a value, and then add to it, and then add the next value to that, and then add the next value to that, and so on, going through all values in my list/folder. I think this would be referred to as a "cumulative sum". So let's say I have a list of numbers:

list = [1,4,6,3,7,5,2,8,9]

I want to iterate over each value in this list and add all values to the previous like so:

for value in list:
    1 + value
    sum = 1 + value
    print(sum)

or something like that? I tried this, and it just adds one to each value in the list, when I want a single final cumulative sum produced. I am really confused about if my logic is correct there. Is this how I would carry out this cumulative sum operation? I am trying to take 1, and then add 4, and then add 6 to that, and then add 3 to that sum, and then add 7 to that sum, and so on. I realize one could also just add all of these values at once, but I am trying to understand how to do this with a for loop. Thanks!

🌐
Altcademy
altcademy.com › blog › how-to-sum-a-list-in-python
How to sum a list in Python - Altcademy.com
September 10, 2023 - For each iteration, we add the current number to the total. The += operator is a shorthand for total = total + number. Python also provides a more concise way to sum a list using a technique called list comprehension.
🌐
Programiz
programiz.com › python-programming › examples › sum-natural-number
Python Program to Find the Sum of Natural Numbers
Initially, the sum is initialized to 0. And, the number is stored in variable num. Then, we used the while loop to iterate until num becomes zero. In each iteration of the loop, we have added the num to sum and the value of num is decreased by 1. We could have solved the above problem without ...