**Problem :**

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

0 votes

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

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