-6

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)?

Jules Dupont
  • 7,259
  • 7
  • 39
  • 39

1 Answers1

0

Using a for loop:

acc = 0
for num in list1:
    acc += num

Another approach:

acc = sum(list1)
Giovanni Rescia
  • 605
  • 5
  • 15