• Register
0 votes
125 views

Problem :

Python Program to Calculate the Average of Numbers in a Given List

6.9k points

2 Answers

0 votes

Solution :

PFB code of the Python Program to Calculate the Average of Numbers in a Given List. 

 n=int(input("Enter the number of elements to be inserted: "))
a=[]
for i in range(0,n):
    elem=int(input("Enter element: "))
    a.append(elem)
avg=sum(a)/n
print("Average of elements in the list",round(avg,2))
38.6k points
edited by
1 vote

Solution:

As I can see someone already answered your question. Well, using the range() function could be one of the solutions to find the average but it is treated as a naive method. If you are having an integer list you could go with either the mean() function:

from statistics import mean 
def Average(lst): 
	return mean(lst)
is_a_lst = [15, 9, 5, 4, 35, 10, 62, 9] 
average = Average(is_a_lst) 
print(round(average, 2))

The in-built mean() function comes with the statistics module and able to calculate the mean or average value of a list in Python. The round() function returns the value as a floating number.

Another possible way to do this is by using the sum() and len() functions. The sum() function adds all the elements in a list and the len() function finds the length of that list. Now, we can easily find the average value this way:

def Average(lst):
	return sum(lst) / len(lst)
is_a_list = [15, 9, 5, 4, 35, 10, 62, 9]
average = Average(is_a_list)
print(round(average, 2))

 

94.2k points

Related questions

0 votes
1 answer 110 views
110 views
Problem : Facing following issue related to list in Python given that a refers to a list, write the necessary code to reverse the elements of the list.
asked Nov 16, 2019 peterlaw 6.9k points
1 vote
1 answer 30 views
30 views
Problem: I have a list that contains the values: list = [2, 4, 8, 16, 32, 2, 64, 128, 2, 256, 512, 2] I wrote a python program for this where I could find out how many times a value appears in a list python: def count_appearence(list, z): check = ... count_appearence(list, z))) NameError: name 'z' is not defined What is the problem with my code. Please help me solve this thing. Thanks in advance.
asked May 13 samhaz 5k points
0 votes
2 answers 28 views
28 views
Problem: I am a newbee in python programming. I tried to remove all occurrences of a character in a list python. So I wrote a code: Nums = [11, 22, 11, 33, 11, 44, 11, 55] a = 11 print ("Original Numbers:") print (Nums) i=0 length = len(Nums) while(i< ... line 8, in <module> if(Nums[i]==a): IndexError: list index out of range Why I am getting this output but not the actual result. Please help
asked Mar 22 LizzyM 6.1k points
0 votes
1 answer 38 views
38 views
Problem : How to remove all occurrences of a value from a list? I tried in Python remove() will remove the first occurrence of value in a list. How to remove all occurences?
asked Oct 22, 2019 peterlaw 6.9k points
1 vote
1 answer 28 views
28 views
Problem: I was trying to check if an element does exist in a list using a pyhton program: primary_list = [ 10, 12, 16, 6, 20, 33 ] a = input("Please enter a number to check if this exists in the list: ") print("Checking if input exists in this list : ") for ... not giving any output that is right. What should I do to fix this and how to check if an element does exist in a list. Thanks in advance.
asked May 16 samhaz 5k points