• Register
0 votes

Problem :

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

6 5 3
6,930 points

Please log in or register to answer this question.

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: "))
for i in range(0,n):
    elem=int(input("Enter element: "))
print("Average of elements in the list",round(avg,2))
9 7 4
38,600 points
1 vote


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))


13 9 6
94,240 points

Related questions

0 votes
1 answer 128 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
0 votes
0 answers 2 views
Problem: For a list ["foo","bar","baz"]and a list item "bar", what is the cleanest way to get its index (1) in python?
asked 23 hours ago sasha 12.9k points
0 votes
1 answer 6 views
Problem: items = [] items.append("apple") items.append("orange") items.append("banana") // FAKE METHOD:: items.amount() // should return 3 How can I do that?
asked Feb 15 sasha 12.9k points
1 vote
1 answer 80 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, 2020 samhaz 5k points
0 votes
2 answers 38 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, 2020 LizzyM 6.1k points
0 votes
1 answer 66 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
0 votes
1 answer 9 views
Problem: Hi! I am a beginner and got stuck on an assignment in which I am asked to write a python function that takes three arguments and after performing some calculation, should tell whether the three values can be used to create a triangle. I am unable to build the logic. Please help!
asked 2 days ago Code Learner 6k points
0 votes
2 answers 12 views
Problem: How a list is the most flexible data structure in Python. Whereas, a 2D list which is commonly known as a list of lists, is a list object where every item is a list itself - for example: [[1,2,3], [4,5,6], [7,8,9]].
asked Jan 26 Mashhoodch 9.1k points
1 vote
1 answer 52 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, 2020 samhaz 5k points
1 vote
1 answer 45 views
Problem: I have 2 list that consist of some name and some numbers: list_1 = ['samhaz', 'porto', 'chris', 'evans'] list_2 = ['33', '14', '26', '30'] I tried to add this two list together(the names with the numbers). For that I wrote a python program: ... ' object has no attribute 'add' What is the problem in my program and how can I add those 2 list(names and numbers) together. Thanks in advance.
asked May 13, 2020 samhaz 5k points