Python average of a list
Let's assume we have these numbers in a list:
data = [0,1,8,9,5,3,10,2,2,5]
The mean is the sum of our list divided by the nr of elements
Simple put mean = (0 + 1 + 8 + 9 + 5 + 3 + 10 + 2+ 2 + 5) / 10
So let's count the list elements
count = len(data)
Display the count
print(f"Data has {count} items")
In python you can use sum function to add all elements of a list
And now you can find out the mean
mean = sum(data) / count
# print(f"Average for data list is {mean}")
If you are using Python >= 3.4 then you can use the statistics library to easily compute the mean.
import statistics
mean = statistics.mean(data)
print(f"Average for data list is {mean}")
You can do the same thing in numpy library.
So first make sure you install numpy.
pip install numpy
Then you can use the library by importing first
import numpy
Use the mean method to find out the data mean
mean = numpy.mean(data)
print(f"Average for data list is {mean}")