# Python: Sum integers from 1 to N the easiest way

( 73 Articles)

This’s a short and straight-to-the-point guide to calculating the sum of integers from 1 to a given positive integer (let’s call it “n”) in Python.

**The code: **

```
def sum(n):
result = 0
for i in range(n):
result += i
print(str(result))
# n = 100
sum(100)
# n = 1000
sum(1000)
```

**Output:**

```
4950
499500
```

**Further reading:**

- How to Install Python Libraries in Google Colab
- List, Dict, and Set Comprehensions in Python 3
- How to Check Whether an Object is Iterable in Python 3
- Examples of using Lambda Functions in Python 3
- VS Code: How to comment out a block of Python code

You can also check out our Machine Learning category page or Python category page for more tutorials and examples.

Subscribe

0 Comments