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**:

- 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.

**Further reading**

- Try to solve Python Exercise for beginners and Quiz 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 = 10
sum = 0
for num in range(0, n+1, 1):
sum = sum+num
print("SUM of first ", n, "numbers is: ", sum )
```

**Output**:

Sum of first 10 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 = 10
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**:

calculate an average of first n natural numbers 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 = 10
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**:

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("\n")
print("All entered numbers ", numberList)
# Calculating the sum of all user entered numbers
sum1 = 0
for num in numberList:
sum1 += int(num)
print("Sum of all entered numbers = ", sum1)
# Calculating the average of all user entered numbers
average = sum1 / len(numberList)
print("Average of all entered numbers = ", average)
```

**Output**:

Enter numbers separated by space 5 10 15 20 25 All entered numbers ['5', '10', '15', '20', '25'] Sum of all entered numbers = 75 Average of all entered numbers = 15.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**:

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**:

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**:

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)
```