First page Back Continue Last page Overview Graphics

Sum of Numbers with for

n = 100

sum = 0

i = 1

while i <= n:

sum += i

i += 1

print(sum)

equivalent

n = 100

sum = 0

for i in range(1,n+1):

sum += + i

print(sum)

The sum of the numbers from 1 to 100 can be calculated like this: