# python program to calculate the average of numbers in a given list

125 views

## Problem :

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

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

## Related questions

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.
1 vote
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.