How to **find the sum and average of first n natural numbers in Python**? In this article, we are going to write a Python code to calculate the sum and average of the positive numbers starting from 1 to the given number (n) using for loop.

**Goals of this article**: In this article, we will see various Python examples that cover the following.

- Calculate the sum and average of first n natural numbers
**using loop and range function**. - Calculate the sum and average of first n natural numbers using a
**mathematical formula**in the program. - Calculate the sum and average of
**any user-entered numbers**. - Calculate the
**sum and average of a given list**in Python - Calculate sum using the
**built-in sum function**in Python

Now let’s see each one by one.

**Free Bonus**:

- Try to solve a Python Basic Exercise for Beginners and Basic Exercise for Beginners
- Also, explore All Python Exercises and Python Quizzes for
**Beginners**to Practice and master Python

## Algorithm to calculate the sum and average of first n natural numbers

- Allows a user to enter the number (n) he wishes to calculate the sum and average. The program accepts user input using the input function.
- Next, run loop till the entered number using the for loop and range() function.
- Next, calculate the sum using a
`sum = sum + current number`

formula. - At last, after the loop ends, calculate the average using
`average = sum / n`

.`n`

is a number entered by the user.

### Python Program to calculate the Sum

n = input("Enter Number to calculate sum") n = int (n) sum = 0 for num in range(0, n+1, 1): sum = sum+num print("SUM of first ", n, "numbers is: ", sum )

**Output**: Run Online

Enter Number to calculate sum 10 Sum of first 5 number is: 55

Note: The **above program loops from 1 to the number entered by the user and adds all numbers to the variable sum**.

### Python Program to calculate average

print ("calculate an average of first n natural numbers") n = input("Enter Number ") n = int (n) average = 0 sum = 0 for num in range(0,n+1,1): sum = sum+num; average = sum / n print("Average of first ", n, "natural number is: ", average)

**Output**: Run Online

calculate an average of first n natural numbers Enter Number 10 Average of first 10 natural number is: 5.5

## Calculate the sum and average of first n natural numbers using formula

In the above program, we calculated the sum and average using loop and range function. Let’s see how to **calculate the sum and average directly using a mathematical formula**.

The sum of the first n natural number = ** n * (n+1) / 2**, for n a natural number.

the average of first n natural number =

`( n * (n+1) / 2) / n`

Let’s see the example now.

n = input("Enter a number to calculate sum") n = int (n) sum = n * (n+1) / 2 average = ( n * (n+1) / 2) / n print("Sum of fthe irst ", n, "natural numbers using formula is: ", sum ) print("Average of the first ", n, "natural numbers using formula is: ", average )

**Output**: Run Online

Enter a number to calculate sum 10 Sum of the first 10 natural numbers using formula is: 55.0 Average of the first 10 natural numbers using formula is: 5.5

## Calculate the sum and average of multiple user-entered numbers

**For example, you want to calculate the sum and average of any five numbers entered by the user**. i.e., If you want to calculate the sum and average or percentage of multiple user-entered numbers, then please refer to the following program.

numbers = input("Enter numbers separated by space ") numberList = numbers.split() print("All entered numbers ", numberList) #Calculating the sum of all user entered numbers sum = 0 for num in numberList: sum += int(num) print("Sum of all entered numbers = ", sum) avg = sum/len(numberList) print("Average of all entered numbers = ", avg)

**Output**: Run Online

Enter numbers separated by space 12 22 32 42 52 62 All entered numbers ['12', '22', '32', '42', '52', '62'] Sum of all entered numbers = 222 Average of all entered numbers = 37.0

Read more on how to accept the list as an input from the user.

## Calculate the sum and average of a given list in Python

Let see you have a list of numbers and you want to calculate the sum of numbers present in the list.

- All you need to do is to iterate a list using a for loop and add each number to a sum variable.
- To calculate the average divide the sum by the length of a given list(total numbers in a list)

Let see this with an example.

Program 1:

sum = 0 list = [11,22,33,44,55,66,77] for num in list: sum = sum +num average = sum / len(list) print ("sum of list element is : ", sum) print ("Average of list element is ", average )

**Output**: Run Online

sum of list element is : 308 Average of list element is 44.0

## Calculate sum using the built-in sum function in Python

Let’s see how to use a `sum()`

function to calculate the sum.

print ("Sum of range of numbers: ", sum(range(1, 5)))

**Output**: Run Online

Sum of range of numbers: 10

## Python while loop to calculate sum and average

You can also use the while loop to calculate the sum and average of n numbers. Here, n can be 2, 5 or any number. Follow these steps:

- Take a value of n =20
- Run while loop until n is greater than zero
- Add the current value of n to sum variable. Also, decrement n by 1 in while loop body
- calculates the average by dividing the sum by total numbers.

Let see the demo to calculate the sum and average using the while loop.

n = 20 total_numbers = n sum=0 while (n >= 0): sum += n n-=1 print ("sum using while loop ", sum) average = sum / total_numbers print("Average using a while loop ", average)

**Output**: Run Online

sum using while loop 210 Average using a while loop 10.5

### Practice Problem: Add Two Matrices in Python

matrixOne = [[6,9,11], [2 ,3,8]] matrixTwo = [[15,18,11], [26,16,19]] result = [[0,0,0], [0,0,0]] # First iterate rows for i in range(len(matrixOne)): # Second iterate columns for j in range(len(matrixOne[0])): result[i][j] = matrixOne[i][j] + matrixTwo[i][j] print("Addition of two Matrix In Python") for res in result: print(res)

## Next Steps

To practice what you learned in this article, I have created a Python Basic **Quiz and Exercise**.

- Solve our
**Python essential exercise for beginners**to have a better understanding of Python’s basic concepts. - Solve our
**Basic Python Quiz for beginners**to test your fundamental Python skills.

Let me know your comments and feedback in the section below.