How to Sum all Numbers in a Range in Python

Borislav Hadzhiev

Last updated: Apr 9, 2024

Reading timeยท4 min

- Sum all numbers in a range in Python
- Sum the numbers in a range with a step in Python
- Sum the Integers from 1 to N in Python
- Sum the numbers in a range that are divisible by N

**To sum all numbers in a range:**

- Use the
`range()`

class to get a range of numbers. - Pass the
`range`

object to the`sum()`

function. - 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)`

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:

Name | Description |
---|---|

`start` | An integer representing the start of the range (defaults to `0` ) |

`stop` | Go up to, but not including the provided integer |

`step` | Range 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:

Name | Description |
---|---|

iterable | the iterable whose items to sum |

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

.

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)`

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)`

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`

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.

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.

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

- Sum all values in a Dictionary or List of Dicts in Python
- Sum the elements of a Tuple or List of Tuples in Python
- Subtract a Value from every Number in a List in Python
- How to sum in a For or a While Loop in Python
- How to sum a List of Strings in Python
- Python: Find range overlap and Check if Ranges overlap