1import statistics
2
3a = [1,2,3,4,5]
4
5mean = statistics.mean(a)
6#Similar for other values such as variance, standard deviation
1import numpy as np
2values=[1,10,100]
3print(np.mean(values))
4values=[1,10,100,np.nan]
5print(np.nanmean(values))
1def calculate_mean(n):
2 s = sum(n)
3 N = len(n)
4
5 mean = s / N
6
7 return mean
1>>> import statistics
2
3>>> statistics.mean([4, 8, 6, 5, 3, 2, 8, 9, 2, 5])
45.2
5
1>>> def my_mean(sample):
2... return sum(sample) / len(sample)
3...
4
5>>> my_mean([4, 8, 6, 5, 3, 2, 8, 9, 2, 5])
65.2
7
1def cal_average(num):
2 sum_num = 0
3 for t in num:
4 sum_num = sum_num + t
5
6 avg = sum_num / len(num)
7 return avg
8
9print("The average is", cal_average([18,25,3,41,5]))
10