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 N natural numbers using for loop.

- The article is part of
**Python Basics**. - Solve our
**Python Basic Exercise for Beginners**and**Python Quiz for Beginners**to practice Python essential skills.

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

- This Python program allows the user to enter the number (n) he wishes to calculate the average. The program accepts user input using the input function.
- Next, the user has to enter the individual number up to a declared number.
- Iterate till the entered number using the for loop and range() function.
- Next, calculate the sum using a
`sum = sum + current number`

formula. - Later calculate the average using
`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) average = 0 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 5 Sum of first 5 number is: 15

## 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, "number is: ", average)

**Output**: Run Online

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

## 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.
- Calculate the length of a given list(total numbers in a list) and divide the sum by total length to calculate average.

Let see this with an example.

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.