Python Sum 1 to N
accumulatorPractice

Add numbers in a loop — the accumulator pattern

Start with total = 0. Each loop adds the next i. This is how apps sum cart prices, steps, or scores.

Picture this: a running total

Start at 0. Each loop adds the next number into total.

total = 0empty bucket
→
add itotal += i
→
next irepeat
→
print(total)final sum

In easy words: What is this loop doing?

This loop keeps adding the next number into a running total. It turns many small values into one final sum.

Where this is used

Used in cart totals, score totals, attendance totals, step counters, and report generation where data is accumulated over time.

Real world: piggy bank

You drop ₹1, then ₹2, then ₹3 coins. After each drop you update the running total. For n=5: 1+2+3+4+5 = 15.

Watch total grow

total (accumulator) 0
0 + 1 + 2 + …
1total = 0
2for i in range(1, n + 1):
3total += i

Example Program

1n = int(input())
2total = 0
3for i in range(1, n + 1): total += i
4print(total)