Sum the integers from 1 to n in Python

avatar

Borislav Hadzhiev

Last updated: Jul 4, 2022

banner

Photo from Unsplash

Sum the integers from 1 to n in Python #

Multiply by n + 1 and floor-divide by 2 to get the integers from 1 to n, e.g. total = n * (n + 1) // 2. The result will be the sum of the integers from 1 to n (including n).

main.py
n = 5 total = n * (n + 1) // 2 print(total) # 👉️ 15 # -------------------------------- total_2 = sum(range(1, n + 1)) print(total_2) # 👉️ 15 # 👇️ [1, 2, 3, 4, 5] print(list(range(1, n + 1)))

The first example multiplies n by n + 1 and floor-divides by 2 to get the sum of the integers from 1 to n.

Division / of integers yields a float, while floor division // of integers result in an integer.

The result of using the floor division operator is that of a mathematical division with the floor() function applied to the result.

If you don't want to use a formula, use the range() class.

To sum the integers from 1 to n:

  1. Pass 1 and n+1 to the range class, e.g. range(1, n + 1).
  2. Pass the range object to the sum() function.
  3. The sum function will sum the integers from 1 to n.
main.py
n = 5 total_2 = sum(range(1, n + 1)) print(total_2) # 👉️ 15 # 👇️ [1, 2, 3, 4, 5] print(list(range(1, n + 1)))

We used the range() class to sum the numbers from 1 to n.

The range class is commonly used for looping a specific number of times in for loops and takes the following arguments:

NameDescription
startAn integer representing the start of the range (defaults to 0)
stopGo up to, but not including the provided integer
stepRange will consist of every N numbers from start to stop (defaults to 1)
Notice that we added 1 to the stop value. The stop argument is exclusive, so we had to add 1 to include n in the result.

If you only pass a single argument to the range() constructor, it is considered to be the value for the stop parameter.

main.py
n = 5 total_2 = sum(range(n + 1)) print(total_2) # 👉️ 15 # 👇️ [0, 1, 2, 3, 4, 5] print(list(range(n + 1)))
The example shows that if the start argument is omitted, it defaults to 0 and if the step argument is omitted, it defaults to 1.

Since the start argument of the range is 0, you can omit it and you'd get the same result.

If values for the start and stop parameters are provided, the start value is inclusive, whereas the stop value is exclusive.

If the value for the stop parameter is lower than the value for the start parameter, the range will be empty.

main.py
# 👇️ [] print(list(range(5, 1))) total = sum(range(5, 1)) print(total) # 👉️ 0

The sum function can be used to calculate the sum of the numbers in the range.

The sum function takes an iterable, sums its items from left to right and returns the total.

The sum function takes the following 2 arguments:

NameDescription
iterablethe iterable whose items to sum
startsums the start value and the items of the iterable. sum defaults to 0 (optional)

If you need to get a range with a step, pass a value for the third argument of the range() class.

main.py
start = 1 stop = 5 step = 2 total_2 = sum(range(start, stop + 1, step)) print(total_2) # 👉️ 9 (1 + 3 + 5) # 👇️ [1, 3, 5] print(list(range(start, stop + 1, step)))

When the step argument is provided, the range will consist of every N numbers from start to stop.

The value for the step argument defaults to 1.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.