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 number using for loop.

- This article is part of our series
**Python Basics**. **Solve our Free Python Basic Exercise for Beginners**to practice Python basic skills.- Solve our Free
**Python Quiz For Beginners.**

## The 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: **Execute 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: **Execute 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: **Execute Online**

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

## Calculate sum using 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: **Execute Online**

Sum of range of numbers: 10

## Python while loop to calculate sum and average

You can also use 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
- calculate the average by dividing the sum by total numbers.

Let see the demo to calculate the sum and average using 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: Execute Online

sum using while loop 210 Average using a while loop 10.5

## Next Steps

**Practice Problem: Add Two Matrices in Python**

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

**Solve our Free Python essential exercise for beginners**. This Exercise will help you to have a better understanding of Python basic concepts.- Also, Solve our
**Basic Python Quiz for beginners**to test your fundamental Python skills.

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

VED PRAKASH says

While calculating average using while loop why it’s necessary to divide the “sum” by “total_numbers” instead of “n” even if we take n >=1.

Please explain.

Vishal says

Hey Ved Prakash,

we decrementing the value of n in each iteration i.e. n-=1. so and the end of the last iteration of while loop n will become ZERO.

that’s why before the start of the loop iteration we are taking the value of n into another variable which is nothing but total_number.

vmngh says

vhn

chand says

Python code

Need help in writing code in finding average using while loop.

Can you help with the code

Question

Write a program that first prompts the user to enter the number of numbers to be entered and then uses a while loop to repeatedly prompt the user for those numbers and adds the numbers to a running total. When the correct number of numbers have been entered, the program should print the average.

You can assume the number of numbers entered is an integer greater than zero and that each subsequent number is a float.