statistics.quantiles(data, *, n=4, method='exclusive')
The statistics.quantiles function returns the quantiles corresponding to the numbers contained in the iterable data. If the parameter n takes the value 4, the function returns the numerical values corresponding to the first, second and third quartiles. If n takes the value 100, the function returns the numeric values corresponding to the percentiles ranging from 1 to 100. In general, for a value of the parameter n, the function will return the highest n-1 quantiles.
- data: Iterable from whose numerical values you want to obtain the quantiles.
- n: Optional argument. Number of quantiles to identify. The function will return the numeric values of the upper n-1 quantiles. By default it takes the value 4.
- method: Optional argument that can take the values "exclusive" or "inclusive" and that defines the method of calculating the quantiles. By default it takes the value "exclusive".
The statistics.quantiles function returns a list with the n-1 requested quantiles.
We can generate a list of 20 random numbers between 0 and 100 with the following code:
sorted(data)
Now we can get the upper three quartiles using the statistics.quantiles function:
The quantiles returned by the function do not have to belong to data:
...and they can be both integer and real values: