Visit every box, one after another
Think of a shopkeeper totting up a bill: they go down the list, one item at a time, from the first to the last. They never skip one and never count one twice.
A loop does exactly that with an array. It starts at box 0, does something with the value there, moves to box 1, and keeps going until it has visited the last box. Then it stops, before it runs off the end.
From box 0 to the last box: every box visited exactly once.
Stop at the length: there's no box with that number.
Add as you go
3
01
14
21
35
4i
total = 0 + 3 = 3
Move 1 of 5
Keep a running total
Before the loop starts, set a total to 0, like clearing a calculator. Each time the loop visits a box, add that box's value to the total. When the loop ends, the total is the sum of everything.
An empty array just means the loop never runs, so the total stays at 0, which is the right answer.
Start at 0, before the loop: then add one value per visit.
total = 0
for x in nums:
total += x
return total