You can complete the code to calculate the sum of your list using a loop. Here’s how to do it:
numbers = [2, 4, 6, 8, 10]
total = 0
# Add each number to total using a loop
for num in numbers:
total += num
print("Sum is:", total)
In this code:
- The
for
loop iterates over each number in thenumbers
list. - The
total += num
line adds the value ofnum
tototal
for each iteration. - Finally, it prints the total sum of the numbers in the list.
When you run this code, it will output:
Sum is: 30