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. Answer from synthphreak on reddit.com
๐ŸŒ
University of Vermont
uvm.edu โ€บ ~cbcafier โ€บ cs1210 โ€บ book โ€บ 11_loops โ€บ loops_and_summation.html
Math and Python: loops and summation โ€“ Clayton Cafiero
While we have the Python built-in function sum() which sums the elements of a sequence (provided the elements of the sequence are all of numeric type), itโ€™s instructive to see how we can do this in a loop (in fact, summing in a loop is exactly what the sum() function does). Hereโ€™s a simple example. t = (27, 3, 19, 43, 11, 9, 31, 36, 75, 2) sum_ = 0 for n in t: sum_ += n print(sum_) # prints 256 assert sum_ == sum(t) # verify answer
๐ŸŒ
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!

Discussions

python - How to sum all the answers from for loop? - Stack Overflow
Well I'm quite new to python and I'm struggling with this at the moment. I need to sum up all the answers(returns) from this for loop but I don't really know how to do this. a = int(input('Enter a ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Python summing in loop vs sum() performance
Hey everyone! Recently I was playing with python. And noticed in my opinion strange behaviour. I have 4 functions: def t1(): s = time.perf_counter_ns() counter = 0 for _ in range(10_000_000): counter += 1 e = time.perf_counter_ns() print(counter) return int((e - s) / 1_000_000) def t2(): s ... More on discuss.python.org
๐ŸŒ discuss.python.org
3
0
April 3, 2023
python - Get sum of numbers using a Function and For Loop - Stack Overflow
The function sumAll needs to use a for loop to carry out this summation, and it will have to use a sum variable that increases in value over each iteration of the for loop. More on stackoverflow.com
๐ŸŒ stackoverflow.com
How does sum function work in python with for loop - Stack Overflow
I was using sum function in python, and i am clear about it's general structure sum(iterable, start) , but i am unable to get the logic behind the following code test = sum(5 for i in range(5) ) p... More on stackoverflow.com
๐ŸŒ stackoverflow.com
December 31, 2018
๐ŸŒ
Bobby Hadz
bobbyhadz.com โ€บ blog โ€บ python-sum-in-for-loop
How to sum in a For or a While Loop in Python | bobbyhadz
Iterate for as long as the number is greater than 0. On each iteration, decrement the number by 1. On each iteration, increment the total by the number. ... Copied!sum_of_numbers = 0 num = 5 while num > 0: # ๐Ÿ‘‡๏ธ reassign sum to sum + num ...
๐ŸŒ
Real Python
realpython.com โ€บ python-sum-function
Python's sum(): The Pythonic Way to Sum Values โ€“ Real Python
July 21, 2023 - 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.
๐ŸŒ
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 ...
Find elsewhere
๐ŸŒ
Python.org
discuss.python.org โ€บ python help
Python summing in loop vs sum() performance - Python Help - Discussions on Python.org
April 3, 2023 - Hey everyone! Recently I was playing with python. And noticed in my opinion strange behaviour. I have 4 functions: def t1(): s = time.perf_counter_ns() counter = 0 for _ in range(10_000_000): counter += 1 e = time.perf_counter_ns() print(counter) return int((e - s) / 1_000_000) def t2(): s = time.perf_counter_ns() counter = sum(1 for _ in range(10_000_000) e = time.perf_counter_ns() print(counter) return int((e - s) / 1_000_000) def t11():...
๐ŸŒ
PYnative
pynative.com โ€บ home โ€บ python โ€บ python program to calculate sum and average of first n natural numbers
Python program to calculate sum and average of first n natural numbers
June 16, 2021 - In each iteration, add the current value of n to the sum variable and decrement n by 1. Calculates the average by dividing the sum by n (total numbers). n = 20 total_numbers = n sum = 0 while n >= 0: sum += n n -= 1 print("sum =", sum) # Output ...
๐ŸŒ
Python Guides
pythonguides.com โ€บ sum-elements-in-list-in-python-using-for-loop
How To Sum Elements In A List In Python
June 3, 2024 - Well, summing elements within a list is a fundamental operation of Python. As we have seen, this can be done in many different ways, depending on the situation and aesthetic preferences. Be it through a very simple for loop, using the sum() function, the perfect, concise way of using list comprehensions, or the more flexible method with reduce().
๐ŸŒ
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.
๐ŸŒ
PyTutorial
pytutorial.com โ€บ python-for-loop-sum-numbers-in-a-range
PyTutorial | Python For Loop: Sum Numbers in a Range
March 28, 2026 - # Sum even numbers from 2 to 30 even_sum = 0 for even_num in range(2, 31, 2): even_sum += even_num print(f"Sum of even numbers (2-30): {even_sum}") ... Using a for loop is an excellent learning exercise. It reinforces how loops work with sequences. You see how a variable's state changes over time. It is also very readable. The code clearly shows the intention: "for each number in this range, add it to the total." While Python has a built-in sum() function, understanding the loop is crucial.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ sum-function-python
sum() function in Python - GeeksforGeeks
January 2, 2025 - ... In this, the code first defines a list of numbers. It then initializes a variable called total to 0. The code then iterates through the list using a for loop, and for each number in the list, it adds that number to the total variable.
๐ŸŒ
Quora
quora.com โ€บ How-do-I-find-the-sum-of-the-first-100-numbers-using-a-loop-in-Python
How to find the sum of the first 100 numbers using a loop in Python - Quora
Answer (1 of 2): The very basic way to find the sum of the first 100 number in python is: [code]num = 100 sum = 0 for i in range(num + 1): sum + = i print(sum) [/code]This will return an output: [code]15 [/code]Let's generalize this into a method that accepts an input [code ]num[/code]. This [...
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ methods โ€บ built-in โ€บ sum
Python sum()
The sum() function adds the items of an iterable and returns the sum. In this tutorial, we will learn about the sum() function with the help of examples.