Sometimes, it is necessary to find the average of a given list of numbers.
In Python, we can calculate the average of a list using the sum and length of the list.
1.sum()
2.len()
3.numpy.average
import numpy as np# list l1l1 = [5, 9, 8, 6]# length of the listlength = len(l1)# sum of values of the listsum_list = sum(l1)# Averageavg = sum_list / lengthprint("Average =", avg)## using numpydata = np.array(l1)print("avg using numpy = ", np.average(data))
Free Resources