How to Find the Sum of Natural Numbers using Python?


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)


Updated on: 21-Feb-2020

925 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements