This is a Python Program to Calculate the Average of Numbers in a Given List.
The program takes the elements of the list one by one and displays the average of the elements of the list.
- Take the number of elements to be stored in the list as input.
- Use a for loop to input elements into the list.
- Calculate the total sum of elements in the list.
- Divide the sum by total number of elements in the list.
- Exit.
numbers = []
n = int(input("Enter the number of elements: "))
for i in range(0, n):
elem = int(input("Enter the elements: "))
numbers.append(elem)
avg = sum(numbers)/n
print("The created list: ",numbers)
print("The average = ",avg)
- User must first enter the number of elements which is stored in the variable n.
- The value of I ranges from 0 to the number of elements and is incremented each time after the body of the loop is executed.
- Then, the element that the user enters is stored in the variable elem.
- a.append(elem) appends the element to the list.
- Now the value of i is incremented to 2.
- The new value entered by the user for the next loop iteration is now stored in elem which is appended to the list.
- The loop runs till the value of i reaches n.
- sum(a) gives the total sum of all the elements in the list and dividing it by the total number of elements gives the average of elements in the list.
- round(avg,2) rounds the average upto 2 decimal places.
- Then the average is printed after rounding.
numbers = []
n = int(input("Enter the number of elements: "))
for i in range(0, n):
elem = int(input("Enter the elements: "))
numbers.append(elem)
avg = sum(numbers)/n
print("The created list: ",numbers)
print("The average = ",avg)
Enter the number of elements: 4
Enter the elements: 30
Enter the elements: 40
Enter the elements: 10
Enter the elements: 20
The created list: [30, 40, 10, 20]
The average = 25.0
numbers = []
n = int(input("Enter the number of elements: "))
for i in range(0, n):
elem = int(input("Enter the elements: "))
numbers.append(elem)
avg = sum(numbers)/n
print("The created list: ",numbers)
print("The average = ",round(avg, 2))
Enter the number of elements: 4
Enter the elements: 55
Enter the elements: 80
Enter the elements: 23
Enter the elements: 56
The created list: [55, 80, 23, 56]
The average = 53.5
numbers = []
n = int(input("Enter the number of elements: "))
for i in range(0, n):
elem = int(input("Enter the elements: "))
numbers.append(elem)
def findAvg(numbers):
total = 0
for j in numbers:
total += j
avg = total/n
return avg
if __name__ == '__main__':
temp = findAvg(numbers)
print("The Average =",temp)
numbers = []
n = int(input("Enter the number of elements: "))
for i in range(0, n):
elem = int(input("Enter the elements: "))
numbers.append(elem)
def findAvg(numbers):
total = 0
for j in numbers:
total += j
avg = total/n
return avg
if __name__ == '__main__':
temp = findAvg(numbers)
print("The Average =",temp)
Enter the number of elements: 4
Enter the elements: 88
Enter the elements: 70
Enter the elements: 90
Enter the elements: 70
The Average = 79.5
numbers = []
n = int(input("Enter the number of elements: "))
for i in range(0, n):
elem = int(input("Enter the elements: "))
numbers.append(elem)
def findAvg(numbers):
total = 0
for j in numbers:
total += j
avg = total/n
return avg
if __name__ == '__main__':
temp = findAvg(numbers)
print("The Average =",round(temp, 2))
Enter the number of elements: 4
Enter the elements: 55
Enter the elements: 60
Enter the elements: 78
Enter the elements: 90
The Average = 70.75