Beginner Python Exercises

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.

  • Beginner
  • 2 min read
  • 1 runnable example
What you'll learn
  • A running total in a loop
  • sum(range(1, n + 1))
  • The formula n × (n + 1) / 2
n = 10 → 55
Loop
total += i
  • 10 additions
Built-in
sum(range(1, 11))
  • one call
Formula
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.

From a piggy bank to code
total = 0🐷 An empty piggy bank
for number in range(1, limit + 1):🪙 Take each number from 1 to 10 in turn
total += number⬇️ Drop that many coins into the bank
print(f"sum={total}")💰 Count what is inside: 55
Start at zero and add every number once.
In one sentence

The 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

  1. total = 0 starts the running total before the loop.
  2. range(1, limit + 1) gives 1 to 10; the end value is excluded, so + 1 is needed.
  3. total += number adds each number to the total.
  4. After the loop total is 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

Run code →
main.py
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}")
Get a Run button for this example

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.

  1. Change the input and predict the output before running.
  2. Handle empty or invalid input.
  3. Wrap the logic in a function and add more test cases.
Run in the Python compiler →

Common mistakes

Leaving out the last number

range(1, limit) stops at 9.

Problem
for number in range(1, limit):
Fix
for number in range(1, limit + 1):

Resetting the total inside the loop

The total starts again at 0 in every round.

Problem
for number in range(1, limit + 1):
    total = 0
    total += number
Fix
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.

All challenges