Python Program to Read a Number n And Print the Series "1+2+…..+n= "

When it is required to display the sum all the natural numbers within a given range, a method can be defined that uses a loop to iterate over the elements, and returns the sum of these numbers as output.

Below is a demonstration of the same −


 Live Demo

def sum_natural_nums(val):
   my_sum = 0
   for i in range(1, val + 1):
      my_sum += i * (i + 1) / 2
   return my_sum

val = 9
print("The value is ")
print("The sum of natural numbers upto 9 is : ")


The value is
The sum of natural numbers upto 9 is :


  • A method named ‘sum_natural_nums’ is defined that takes a number as parameter.

  • A sum value is defined as 0.

  • A loop is iterated over the number passed as a parameter.

  • The sum is incremented every time a number is encountered.

  • This is returned as output.

  • The value for number of natural numbers whose sum needs to be found is defined.

  • The method is called by passing this number as a parameter.

  • The relevant output is displayed on the console.