1# Example to find average of list
2number_list = [45, 34, 10, 36, 12, 6, 80]
3avg = sum(number_list)/len(number_list)
4print("The average is ", round(avg,2))
5
1# Python program to get average of a list
2
3def Average(lst):
4 return sum(lst) / len(lst)
5
6# Driver Code
7lst = [15, 9, 55, 41, 35, 20, 62, 49]
8average = Average(lst)
9
10# Printing average of the list
11print("Average of the list =", round(average, 2))
12
13# Output:
14# Average of the list = 35.75
1list = [15, 18, 2, 36, 12, 78, 5, 6, 9]
2
3# for older versions of python
4average_method_one = sum(list) / len(list)
5# for python 2 convert len to a float to get float division
6average_method_two = sum(list) / float(len(list))
7
8# round answers using round() or ceil()
9print(average_method_one)
10print(average_method_two)
1l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
2
3# By using the built-in statistics library
4import statistics
5statistics.mean(l) # 20.11111111111111
6
7# By defining a custom function
8def average(my_list):
9 return sum(my_list) / len(my_list)
10average(l) # 20.11111111111111