Python: Sum integers from 1 to N the easiest way

Last updated on October 24, 2020 Guest Contributor Loading... Post a comment

This’s a short and straight to the point guide to calculating the sum of integers from 1 to 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

Related Articles

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x