How to Sum all Numbers in a Range in Python

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
4 min

banner

# Table of Contents

  1. Sum all numbers in a range in Python
  2. Sum the numbers in a range with a step in Python
  3. Sum the Integers from 1 to N in Python
  4. Sum the numbers in a range that are divisible by N

# Sum all numbers in a range in Python

To sum all numbers in a range:

  1. Use the range() class to get a range of numbers.
  2. Pass the range object to the sum() function.
  3. The sum() function will return the sum of the integers in the range.
main.py
start = 1 stop = 5 total = sum(range(start, stop)) print(total) # ๐Ÿ‘‰๏ธ 10 (1 + 2 + 3 + 4)

sum all numbers in range

The code for this article is available on GitHub

We used the range() class to get a range of numbers.

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)

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

main.py
# ๐Ÿ‘‡๏ธ [0, 1, 2, 3, 4] print(list(range(5))) total = sum(range(5)) print(total) # ๐Ÿ‘‰๏ธ 10
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.

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

main.py
# ๐Ÿ‘‡๏ธ [1, 2, 3, 4] print(list(range(1, 5))) total = sum(range(1, 5)) print(total) # ๐Ÿ‘‰๏ธ 10

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)

# Sum the numbers in a range with a step in Python

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 = sum(range(start, stop, step)) print(total) # ๐Ÿ‘‰๏ธ 4 (1 + 3) # ๐Ÿ‘‡๏ธ [1, 3] print(list(range(start, stop, step)))

sum numbers in range with step

The code for this article is available on GitHub

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.

# Creating a reusable function

If you have to do this often, define a reusable function.

main.py
def sum_numbers(start, stop): return sum(range(start, stop + 1)) print(sum_numbers(1, 3)) # ๐Ÿ‘‰๏ธ 6 (1 + 2 + 3) print(sum_numbers(1, 4)) # ๐Ÿ‘‰๏ธ 10 (1 + 2 + 3 + 4) print(sum_numbers(1, 5)) # ๐Ÿ‘‰๏ธ 15 (1 + 2 + 3 + 4 + 5)

creating reusable function

The code for this article is available on GitHub

The function takes start and stop values and sums the numbers from start to stop.

Notice that we added 1 to the stop value to make the range inclusive.

If you want to exclude the last number from the range, remove the addition operator.

main.py
def sum_numbers(start, stop): return sum(range(start, stop)) print(sum_numbers(1, 3)) # ๐Ÿ‘‰๏ธ 3 (1 + 2) print(sum_numbers(1, 4)) # ๐Ÿ‘‰๏ธ 6 (1 + 2 + 3) print(sum_numbers(1, 5)) # ๐Ÿ‘‰๏ธ 10 (1 + 2 + 3 + 4)

# 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.

The result will be the sum of the integers from 1 to N (including N).

main.py
# โœ… sum the integers from 1 to 5 n = 5 total = n * (n + 1) // 2 print(total) # ๐Ÿ‘‰๏ธ 15

sum integers from 1 to n

The code for this article is available on GitHub

The 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.

Here is an example that sums the integers from 1 to 100.

main.py
n = 100 total = n * (n + 1) // 2 print(total) # ๐Ÿ‘‰๏ธ 5050

All we had to do was update the value of n to get the sum of the integers from 1 to 100.

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

# Sum the numbers in a range that are divisible by N

If you need to sum the numbers in a range that are divisible by N, use a while loop.

main.py
def sum_divisible_in_range(start, stop, divisor): while start % divisor != 0: start += 1 return sum(range(start, stop, divisor)) print(sum_divisible_in_range(1, 6, 2)) # ๐Ÿ‘‰๏ธ 6 print(sum_divisible_in_range(1, 7, 3)) # ๐Ÿ‘‰๏ธ 9 print(sum_divisible_in_range(1, 8, 4)) # ๐Ÿ‘‰๏ธ 4
The code for this article is available on GitHub

We used a while loop to iterate until the start value reaches the divisor.

The last step is to create a range with the start, stop and divisor values and sum them.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev