Hello everyone, welcome back to Programming In Python! Here we will learn a simple logic to find the average on N numbers in Python. This program takes max numbers from the user and calculates the sum of all the numbers in a loop and the final obtained sum is divided by the total number of inputs taken. That results in the average of N numbers.
The formula for it is Average = ( n1+n2+n3+.....) / N
, where N is the total number of inputs and n1,n2,n3.. are the values of each input.
“Need help with your python assignment? Find at Domypapers.com
Python program to find the average of N numbers – Code Visualization
You can also watch the video on YouTube here.
Task :
To find an average of N numbers with max numbers when the values are given by the user.
Approach :
- Read an input integer for asking max numbers using
input()
orraw_input()
. - Loop N number of times for taking the value of each number using
input()
orraw_input()
, where N is the value entered in the first step. - In the loop, sum the value of each number entered.
- Finally divide the obtained sum by N, where N is the value entered in the first step.
- The result obtained in the previous step is the average we wanted.
- Print the result.
Program :
num = int(input('How many numbers: ')) total_sum = 0 for n in range(num): numbers = float(input('Enter number : ')) total_sum += numbers avg = total_sum/num print('Average of ', num, ' numbers is :', avg)
Output :
That’s it for this post. Feel free to look at some of the algorithms implemented in Python or some basic Python programs or look at all the posts here.
Course Suggestion
Want to be strong at OOP in Python? If yes, I would suggest you take the course below.
Course:
Object Oriented Programming in Python