Python Program To Calculate Average of Numbers | RRTutors
Published January 07, 2021 In this Python example we will learn how to calculate Average of Numbers from given list.
This Example will Give the Solution by below points
- Take the input for number of elements we need to calculate average
- Then Loop the function to take the input values based on above given number
- Calculate the Sum of total numbers given by the loop
- Divide the Total sum by total number of elements
Program to calculate average of numbers
n=int(input("Enter the number of elements to be inserted: "))
numbers=[]
for i in range(0,n):
take=int(input( "Enter element %s: " % (i+1)))
numbers.append(take)
avg=sum(numbers)/n
print("Average of elements in the list",round(avg,2))
|
Explanation:
- First user will enter the number, how many numbers he wants to calculate. The value will be stored in 'n'
- Now we are creating a loop with start index 0 and range up to given number 'n'
- Now each iteration of loop the value will be taking from input() method and stored it in 'take' variable
- The above loop will be take inputs and store into list 'numbers'
- Then calculating the sum of given elements by 'sum()' method
- Finally we are calculating the average of all numbers by avg=sum(numbers)/n
Output:
Enter the number of elements to be inserted: 10
Enter element 1: 23
Enter element 2: 22
Enter element 3: 1
Enter element 4: 24
Enter element 5: 65
Enter element 6: 34
Enter element 7: 34
Enter element 8: 54
Enter element 9: 64
Enter element 10: 34
Average of elements in the list 35.5
|
Read How to concatenate Strings in Python
Conclusion: We can calculate average of any numbers by above python program to calculate average of numbers in the given list.