3

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.

6
  • 5
    If you're asking why mean isn't a builtin function, then you should probably ask the python devs about that.
    – cs95
    Commented Mar 27, 2019 at 2:30
  • @coldspeed, that's exactly what I meant to ask, but did not know to clarify with the term "built-in". This makes Python syntax somewhat inconsistent without builtin mean()!
    – Nemo
    Commented Mar 27, 2019 at 2:46
  • 2
    @coldspeed Advising OP "ask the python devs" is unlikely to lead to greater understanding on their part. If you didn't just mean to make a joke at their expense, you probably need to more carefully state what you're getting at. Commented Mar 27, 2019 at 18:13
  • @RobertDodier it should've been obvious... But I was trying to get at the fact that this is not really appropriate to ask here, being mostly opinion based (the "why" part, at least).
    – cs95
    Commented Mar 27, 2019 at 18:16
  • 1
    @coldspeed Not obvious to OP, I suspect. If you think the question is too much about opinions, then probably better to just say so. Commented Mar 27, 2019 at 18:23

2 Answers 2

8

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.

8
  • Thanks, Kingsley! But I didn't have to import statistics to find docstring for min() and max()!
    – Nemo
    Commented 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.
    – Kingsley
    Commented Mar 29, 2019 at 0:37
  • 1
    Say you have a library for a screen and a library for a paper-plotter. The draw() function would do different things for the different libraries.
    – Kingsley
    Commented 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
1

@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
1
  • But that would go against the spirit of Python for short and sweet? That is, instead of mean(numbers), we have to make do with sum(numbers)/len(numbers)?
    – Nemo
    Commented Mar 29, 2019 at 0:52

Not the answer you're looking for? Browse other questions tagged or ask your own question.