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 OverflowI'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.
How can i sum the value in list with for loop in python? - Stack Overflow
python - Get sum of numbers using a Function and For Loop - Stack Overflow
Can someone explain to me how to add up all the numbers in a list without using sum?
How to cumulative sum over for loop?
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))
Try this:
ds = [1,2,3,4,5]
total = 0
for i in ds:
total += i * i
print(total)
The output is 55
By for loop
def sumAll(n):
sum_all = 0
for i in range(1, n+1):
sum_all = sum_all + i
return sum_all
number = int(raw_input("Please enter a number: \n"))
print ("The answer is: ") + str(sumAll(number))
Output:
Please enter a number:
10
The answer is: 55
You can also use list Comprehension:
print sum([i for i in range(number+1)])
Output:
55
You can also use a mathematical series formula:
def sumAll(n):
return n * (n + 1) / 2
you can even do it without a loop:
def sumAll(n):
return sum(range(1,n+1))
print(sumAll(10)) # -> 55
if you insist on using a loop:
def sumAll(n):
s = 0
for i in range(1,n+1):
s += i
return s
but the mathematical solution is simply:
def sumAll(n):
return n * (n + 1) / 2
# in python 3:
# return n * (n + 1) // 2
(there are n elements with an average value of (n+1)/2; this will always be an integer since either n or n+1 is even; the proof by induction is often the first example when math students learn about induction...).
Hi there, so I’m currently learning about lists and loops and my friend challenged me to add up a entire list without using sum
So far my code looks like this (just the list as a variable)
ls = [1 , 5, 4, 3, 7]
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!