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
🌐
Reddit
reddit.com › r/learnprogramming › how would i calculate sum from a 'for' loop?
r/learnprogramming on Reddit: how would I calculate sum from a 'for' loop?
September 7, 2022 -

So im making a program that calculates the sum of squared values within a range. This is what I have so far:

def sum_squares():
one = eval(input("Enter lower range?: "))
two = eval(input("Enter upper range?: "))
for x in range(one,two+1):
print(x*x)

I want to put "print(sum(x*x))" at the end but it gives me an error. What do.

🌐
Sololearn
sololearn.com › en › Discuss › 2922107 › given-a-list-of-numbers-calculate-their-sum-using-a-for-loop-output-the-sum-after-the-loop
Given a list of numbers, calculate their sum using a for loop. Output the sum after the loop | Sololearn: Learn to code for FREE!
x = [42, 8, 7, 1, 0, 124, 8897, 555, 3, 67, 99] sum = 0 for n in x: #taking list values to n iteratively. sum=sum+n #adding n value to sum print(sum) #its outside of loop so prints only once after loop completion.
🌐
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
🌐
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 ...
🌐
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!

🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › java basics › for loop
Sum Array of Numbers with for loop - Java Code Geeks
August 20, 2013 - Create a for statement, with an int variable from 0 up to the length of the array, incremented by one each time in the loop. In the for statement add each of the array’s elements to an int sum.
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 42394560 › how-to-sum-array-values-from-a-for-loop
How to sum array values from a for loop
If I've understood you correctly, you want to accept 2 numbers from the user, then sum the output of a calculation 10 times and add it back to the page. Hopefully the below will help you out. function przelicznik(){ var first= parseFloat(document.getElementById("pierwszy").value); var second= parseFloat(document.getElementById("drugi").value); var arr=[]; for(var i = 0; i < 10; i++){ var a=first + second; // 4=2+2 // 8=6+2 var b=first + a; // 6=4+2 // 14=6+8 first = b // first=4 // first=8 //result of 10 times loop put into 'var text'. arr.push(a); } document.getElementById("wynik").innerHTML=arr.reduce((a,b) => a + b); }
🌐
Sololearn
sololearn.com › en › Discuss › 3252806 › how-to-do-the-sum-practice-with-the-for-loop
How to do the Sum practice with the for loop | Sololearn: Learn to code for FREE!
EDIT: I went looking for answers ... put into it and it becomes 5050. This is the code I found. int n; cin >> n; int sum=0; for(;n>0;n--) { sum+=n; } cout << sum; }...
🌐
MathWorks
mathworks.com › matlabcentral › answers › 860750-summation-with-for-loop
Summation with FOR Loop - MATLAB Answers - MATLAB Central
After taking the input value of n from the user,we initiated the sum variable to be zero. We can simply iterate over from 2 to n,calculating the terms as depicted by the formula(where each term is of the form (x/x+1)).
Top answer
1 of 2
2

Yes! Both summations and for loops are designed as a shorthand of saying "I want to do this a whole bunch of times but I don't really want to write it all out. Here's the step to repeat and the pattern each step varies by, have at it ;) ."

I could define some variable x = 1 + 4 + 9 + 16 + 25 + 36 + 49 + 64 + 81 + 100, or I could just say x = [the sum of the squared integers 1 to 10, inclusive]. As you pointed out, this is just a summation: sigma from i=1 to 10 of i^2; if you were to ask a computer scientist to add those numbers together, immediatly (s)he would type something like this:

int x = 0;
for(int i = 1; i <= 10; i++) {
    x += i*i;
}

As I'm sure you recognize, this is a rather trivial example. The power of a for loop comes not from adding a bunch of numbers but from executing statements of code with the altered variable as a parameter. Whereas sigma can be thought of as a function that sums the results of some function (as it really is, in its most general form, sigma from i=[lower bound] to [upper bound] of f(i), where f(x) is some function... sorry about the formating, I'm not sure how to do mathematic symbols on SE), a for loop can perform actions with those values of f(i).

I'm not sure if that makes sense, but I guess the TL;DR summery would be that yes, a summation is just a specific kind of for-loop, but a for-loop is much more expressive as it allows the programmer to define what to do with the values of its incrementing variable(s). I suppose you could say there's greater logical control in a for-loop than you find in a summation.

2 of 2
1

$\sum_{n=a}^b f(n)$

is equivalent to

<?php 
$sum = 0;
for ($n = a; $n <= b; $n++) {
  sum +=f(n);
} 
?>

Or in Python, sum([f(n) for n in range(a,b+1)])

🌐
GeeksforGeeks
geeksforgeeks.org › r language › sum-of-vector-elements-using-a-for-loop
Sum of Vector Elements using a for Loop - GeeksforGeeks
July 23, 2025 - Here's how you may do it: ... # Create a numeric vector of test scores scores <- c(85, 92, 78, 95, 88) # Initialize a sum variable total_score <- 0 # Use a for loop to calculate the sum for (score in scores) { total_score <- total_score + score } # ...
Top answer
1 of 2
3

You need to keep track of a total variable, that you add to for each element's text you want to compare.

You need to convert the text of the element into a number. parseFloat() is one way to do that.

When working with async code (Promises, .then(), etc), you can only use values by chaining another .then() to the promise chain, to make sure the value is available. If you try to log total outside of a .then(), that console.log will execute long before your text totalling code runs.

Summing up, here's an example, that also removes the need for an extra element grab, since it looks like you can use .getText() on a collection:

let total = 0;
element.all(by.tagName('strong')).getText().then(function(strongTags) {
  for (let i = 2; i < strongTags.length; i = i + 3) {
    total += parseFloat(strongTags[i].split(' ')[1]);
  }
  return total;
}).then(function(total) => {
  console.log('The total is', total);
});
2 of 2
1

You can try something like this using .reduce

let sum = element.all(by.tagName('strong')).reduce(function(acc, elem) {
  return elem.getText().then(function(text) {
    var text3 = text2.split(" ");
    var price = text3[1];
    return acc + price;
  });
}, 0);

UPD:

.reduce callback also accepts index if you want to skip particular elements.

let sum = element.all(by.tagName('strong')).reduce(function(acc, elem, index) {
  if ((index - 2) % 3 == 0) {
    return acc + 0;
  }
  return elem.getText().then(function(text) {
    var text3 = text2.split(" ");
    var price = text3[1];
    return acc + price;
  });
}, 0);
🌐
EyeHunts
tutorial.eyehunts.com › home › how to sum a list in python using for loop
How to sum a list in Python using for loop
December 29, 2023 - l = [1, 2, 3, 4, 5] sum_list(l) l = list(map(int, input("Enter numbers separated by spaces: ").split())) sum_list(l) Do comment if you have any doubts or suggestions on this Python list topic ... All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions. Click to share on Facebook (Opens in new window) Facebook · Click to share on WhatsApp (Opens in new window) WhatsApp
🌐
Real Python
realpython.com › python-sum-function
Python's sum(): The Pythonic Way to Sum Values – Real Python
July 21, 2023 - In sum_numbers(), you take an iterable—specifically, a list of numeric values—as an argument and return the total sum of the values in the input list. If the input list is empty, then the function returns 0. The for loop is the same one ...