Whilst one can call min() or max() on their own, function mean() has to rely on other imported packages such as Numpy, that is, np.mean(). If the concepts of min and max are natural for a scale/range, shouldn't it be the middle of the scale/range (that is, mean) considered as natural also? What is the underlying reason for this inconsistency? Please note this is not an opinion-based question, I do really like to know the reason for the exclusion of the mean() function from the base package.
It does have a mean, but it needs to be imported from statistics.
import statistics
numbers = [ 1, 2, 3, 4 ]
print( "mean is ", statistics.mean( numbers ) )
Which outputs:
mean is 2.5
There are a set of "built-in" functions for Python. These functions can be called directly. min() and max() fall into this category. Other functions, "library functions" need to be explicitly imported before they can be used, statistics.mean() is a library function.
If you feel like this begs-the-question, "why have library functions?" - there are hundreds of library functions for Python. It's inefficient to include them into the run-time for every program. I've been programming python for more years than I care to remember, and yet I have never used statistics.mean() before this question.
@Kingsley has a good point, but it would may well be a little easier with doing the logic of it:
numbers = [ 1, 2, 3, 4 ]
print("The mean is", sum(numbers)/len(numbers))
It reproduces:
The mean is 2.5
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With