Sum of Natural Numbers in Python
Add 1 + 2 + ... + n with a loop, with sum(range()) and with Gauss's formula, and check that all three agree.
- A running total in a loop
- sum(range(1, n + 1))
- The formula n × (n + 1) / 2
total += i
- 10 additions
sum(range(1, 11))
- one call
10 * 11 // 2
- no loop at all
Three ways to the same total
The problem
Adding up a range of numbers is a building block of countless programs: totals, averages, scores over days. It is also the first place where you see one problem solved by a loop, by a built-in function and by a formula.
Think of it like this
A running total is like a piggy bank. You start with an empty bank, then drop in 1 coin, then 2 coins, then 3 coins, and so on up to 10 coins. At the end you count what is inside: 55 coins.
total = 0🐷 An empty piggy bankfor number in range(1, limit + 1):🪙 Take each number from 1 to 10 in turntotal += number⬇️ Drop that many coins into the bankprint(f"sum={total}")💰 Count what is inside: 55The sum of the natural numbers from 1 to n is 1 + 2 + ... + n, which a loop computes step by step and the formula n × (n + 1) / 2 gives directly.
Key points
total = 0starts the running total before the loop.range(1, limit + 1)gives 1 to 10; the end value is excluded, so+ 1is needed.total += numberadds each number to the total.- After the loop
totalis 55, the same as 10 × 11 / 2.
When should you use it?
- Running totals in any program: scores, prices, distances.
- Learning the accumulator pattern used in most loops that compute a result.
- Checking a formula against a loop to be sure both are right.
When not to use it
- Adding up an existing list:
sum(values)is shorter and faster. - A huge n such as a billion: the formula gives the answer instantly while the loop takes seconds.
Example code
limit = 10
total = 0
# Add every natural number through the inclusive limit.
for number in range(1, limit + 1):
total += number
print(f"sum={total}")
Result of running the code
sum=55
Code explanation
total starts at 0 before the loop, which is the right starting value for a sum. range(1, limit + 1) produces the numbers 1 to 10, because the end value of a range is not included. Each round adds the current number with total += number, so after ten rounds total holds 55.
Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.
Practice exercises
Modify the runnable example with the exercises below to build understanding beyond copying the result.
- Change the input and predict the output before running.
- Handle empty or invalid input.
- Wrap the logic in a function and add more test cases.
Common mistakes
Leaving out the last number
range(1, limit) stops at 9.
for number in range(1, limit):
for number in range(1, limit + 1):
Resetting the total inside the loop
The total starts again at 0 in every round.
for number in range(1, limit + 1):
total = 0
total += number
total = 0
for number in range(1, limit + 1):
total += number
Finished this lesson?
Mark it as learned to track your progress.
Practice with a challenge
Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.
- Triangular Number Add the integers from 1 through n. Easy
- Sum of Multiples Add values below a limit divisible by any supplied factor. Easy