Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't python have built-in function mean()? [closed]

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.

like image 618
Nemo Avatar asked Oct 28 '25 11:10

Nemo


2 Answers

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.

like image 182
Kingsley Avatar answered Oct 30 '25 02:10

Kingsley


@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
like image 22
U12-Forward Avatar answered Oct 30 '25 01:10

U12-Forward



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!