Beginner Python Exercises

Sum Natural Numbers in Python

A running total is a simple way to reduce a numeric range.

What is Sum Natural Numbers?

A running total is a simple way to reduce a numeric range.

Calculate the sum from 1 through n.

When should you use it?

  • Learn Python syntax through a practical example.
  • Build a foundation for larger programming problems.
  • Test an idea quickly without a local setup.

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}")

Expected output

sum=55

How it works

The loop visits each integer once and adds it to the accumulator.

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 Python IDE →