Python Program To Print Average
In this post, we are going to write a python program to print get the input of data from the user and print its average.
General Idea :
The average of a set of data is the sum of the data and divided by the total number of data.
Pre-Requisites :
- Python Basics
- For loop
n=int(input("Enter the total number of numbers"))
sum1=0
for i in range(0,n):
x=int(input("Enter number "))
sum1=sum1+x
print("The Average is ",sum1/n)
Explanation :
1.First we are inputting the total number of data and storing that to variable 'n'. (Here in this case total number of numbers)
2. Here we are initializing the variable (identifier) sum1 to 0, which would be used to find the sum of the data.
3. Here we are using for loop to input the data and find its sum. The start value is 0. The end value of the loop is n. That means it stops at n-1. The step value is 1 by default. Therefore the loop repeats n times.
4. This statement is written under the for loop created in the above step. That means this statement will repeat n times. Here we are inputting the data and storing that to x. Note x value will be changed when the loop enters the next step.
5. This statement is also written under the for loop. When this statement is executed the inputted data is added to the sum1. We already initialized sum1 to 0. As the loop enters a new step, as the user enters the new data which is stored to x, is added to sum1 successively.
6. This statement is written separately. Here we are printing the output. Average is the division of sum by the number of data. Here the sum is sum1 and the number of data is n in our case. Hence we wrote sum1/n.
Input/Output :
Thank you
Comments
Post a Comment