I'm starting to learn how to code and I've been trying to work out how to sum numbers from a list.
list1 = [1,2,3,4,5]
Using a for
-loop, how would I set a variable to the sum of the list (15)?
I'm starting to learn how to code and I've been trying to work out how to sum numbers from a list.
list1 = [1,2,3,4,5]
Using a for
-loop, how would I set a variable to the sum of the list (15)?
Using a for loop:
acc = 0
for num in list1:
acc += num
Another approach:
acc = sum(list1)