Statistical Functions in Python | Set 1 (Averages and Measure of Central Location)
Last Updated : 23 Jul, 2025
Python has the ability to manipulate some statistical data and calculate results of various statistical operations using the file "statistics", useful in domain of mathematics.
Important Average and measure of central location functions :
1. mean() :- This function returns the mean or average of the data passed in its arguments. If passed argument is empty, StatisticsError is raised.
2. mode() :- This function returns the number with maximum number of occurrences. If passed argument is empty, StatisticsError is raised.
Python
# Python code to demonstrate the working of# mean() and mode()# importing statistics to handle statistical operationsimportstatistics# initializing listli=[1,2,3,3,2,2,2,1]# using mean() to calculate average of list elementsprint("The average of list values is : ",end="")print(statistics.mean(li))# using mode() to print maximum occurring of list elementsprint("The maximum occurring element is : ",end="")print(statistics.mode(li))
Output:
The average of list values is : 2.0
The maximum occurring element is : 2
3. median() :- This function is used to calculate the median, i.e middle element of data. If passed argument is empty, StatisticsError is raised.
4. median_low() :- This function returns the median of data in case of odd number of elements, but in case of even number of elements, returns the lower of two middle elements. If passed argument is empty, StatisticsError is raised.
5. median_high() :- This function returns the median of data in case of odd number of elements, but in case of even number of elements, returns the higher of two middle elements. If passed argument is empty, StatisticsError is raised.
Python
# Python code to demonstrate the working of# median(), median_low() and median_high()# importing statistics to handle statistical operationsimportstatistics# initializing listli=[1,2,2,3,3,3]# using median() to print median of list elementsprint("The median of list element is : ",end="")print(statistics.median(li))# using median_low() to print low median of list elementsprint("The lower median of list element is : ",end="")print(statistics.median_low(li))# using median_high() to print high median of list elementsprint("The higher median of list element is : ",end="")print(statistics.median_high(li))
Output:
The median of list element is : 2.5
The lower median of list element is : 2
The higher median of list element is : 3
6. median_grouped() :- This function is used to compute group median, i.e 50th percentile of the data. If passed argument is empty, StatisticsError is raised.
Python
# Python code to demonstrate the working of# median_grouped()# importing statistics to handle statistical operationsimportstatistics# initializing listli=[1,2,2,3,3,3]# using median_grouped() to calculate 50th percentileprint("The 50th percentile of data is : ",end="")print(statistics.median_grouped(li))