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.
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.
-
Thanks, Kingsley! But I didn't have to import statistics to find docstring for min() and max()!– NemoCommented Mar 27, 2019 at 2:48
-
@Jason See also the Scipy and Numpy packages which have many related functions. A web search will find those packages and much information. Commented Mar 27, 2019 at 18:14
-
1@Jason - I added some notes about library functions. Maybe this helps.– KingsleyCommented Mar 29, 2019 at 0:37
-
1Say you have a library for a
screen
and a library for apaper-plotter
. Thedraw()
function would do different things for the different libraries.– KingsleyCommented Mar 29, 2019 at 0:56 -
1@Jason The reason for some functions being built-in and others being in libraries is a consequence of the history of Python. Originally Python wasn't much of a data-processing language, and later that became important. Choices were made early on in the development of Python to make some things built-in which seemed important at the time;
mean
wasn't one of them. Every language is like that -- choices are made in the language design to emphasize what seemed important at the time. If you study the history of various languages you'll see what different people have considered important. Commented Mar 29, 2019 at 17:16
@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
-
But that would go against the spirit of Python for short and sweet? That is, instead of
mean(numbers)
, we have to make do withsum(numbers)/len(numbers)
?– NemoCommented Mar 29, 2019 at 0:52
mean
isn't a builtin function, then you should probably ask the python devs about that.