How to Find the Sum of Natural Numbers using Python?

PythonServer Side ProgrammingProgramming

You can use while loop to successively increment value of a variable i by one and adding it cumulatively.

s,i=0,0
n=10
while i<n:
i=i+1
s=s+i
print ("sum of first 10 natural numbers",s)

For loop is also used to loop over a range of natural numbers and add them cumulatively.

s=0
for i in range(11):
s=s+i
print ("sum of first 10 natural numbers",s)

Lastly using the built in function sum() also gives sum of a range of numbers

s=sum(range(11))
print ("sum of first 10 natural numbers",s)

Published on 24-Feb-2018 10:25:20