Change language

# Statistical Functions in Python | Set 1 (Average and Center Position Measure)

|

Important mean and measure of central location features :

1.  mean () : — This function returns the average or average value of the data passed in its arguments. If the passed argument is empty, StatisticsError is thrown.

2. mode () : — this function returns a maximum number of occurrences . If the passed argument is empty, StatisticsError is raised.

 ` # Python code to demonstrate how it works ` ` # mean () and mode () `   ` # import statistics for processing statistical operations ` ` import ` ` statistics `   ` # initializing list ` ` li ` ` = ` ` [` ` 1 ` ` , ` ` 2 ` `, ` ` 3 ` `, ` ` 3 ` `, ` ` 2 ` `, ` ` 2 ` `, ` ` 2 ` `, ` ` 1 ` `] ` ` `  ` # using mean () to average the list items ` ` print ` ` (` ` "The average of list values ​​is:" ` `, end ` ` = ` ` "") ` ` print ` ` (statistics. mean (li)) `   ` # using mode () to print the maximum number of list items ` ` print ` ` (` ` "The maximum occurring element is :" ` `, end ` ` = ` `" ") ` ` print ` ` (statistics.mode (li)) `

Exit:

` The average of list values ​​is: 2.0 The maximum occurring element is: 2 `

3. mean -median-mode-in-python-without-libraries/">median () : — this function is used to calculate the mean -median-mode-in-python-without-libraries/">median, that is, the average item. If the passed argument is empty, StatisticsError is called.

4. mean -median-mode-in-python-without-libraries/">median_low () : — This function returns the mean -median-mode-in-python-without-libraries/">median of the data if the number of items is odd, but if the number of items is even, it returns the bottom of the middle two items. If the passed argument is empty, StatisticsError is thrown.

5. mean -median-mode-in-python-without-libraries/">median_high () : — this function returns the mean -median-mode-in-python-without-libraries/">median of the data for an odd number of elements, but for an even number of elements, returns the highest of the middle two elements. If the passed argument is empty, StatisticsError is raised.

 ` # Python code to demonstrate how it works ` ` # mean -median-mode-in-python-without-libraries/">median (), mean -median-mode-in-python-without-libraries/">median_low () and mean -median-mode-in-python-without-libraries/">median_high () `   ` # import statistics for statistical processing ` ` import ` ` statistics `   ` # initializing list ` ` li ` ` = ` ` [` ` 1 ` `, ` ` 2 ` `, ` ` 2 ` `, ` ` 3 ` `, ` ` 3 ` `, ` ` 3 ` `] `   ` # using mean -median-mode-in-python-without-libraries/">median () to display the mean -median-mode-in-python-without-libraries/">median of list items ` ` print ` ` (` ` "The mean -median-mode-in-python-without-libraries/">median of list element is:" ` `, end ` ` = ` ` "") ` ` print ` ` (statistics.mean -median-mode-in-python-without-libraries/">median (li)) `   ` # using mean -median-mode-in-python-without-libraries/">median_low () to print the low mean -median-mode-in-python-without-libraries/">median of list items ` ` print ` ` (` ` "The lower mean -median-mode-in-python-without-libraries/">median of list element is:" ` `, end ` ` = ` `" ") ` ` print ` ` (statistics.mean -median-mode-in-python-without-libraries/">median_low (li)) ` ` `  ` # using mean -median-mode-in-python-without-libraries/">median_high () to print the average mean -median-mode-in-python-without-libraries/">median of list items ` ` print ` ` (` `" The higher mean -median-mode-in-python-without-libraries/">median of list element is: "` `, end ` ` = ` `" ") ` ` print ` ` (statistics.mean -median-mode-in-python-without-libraries/">median_high (li)) `

Output:

` The mean -median-mode-in-python-without-libraries/">median of list element is: 2.5 The lower mean -median-mode-in-python-without-libraries/">median of list element is: 2 The higher mean -median-mode-in-python-without-libraries/">median of list element is: 3 `

6. mean -median-mode-in-python-without-libraries/">median_grouped () : — This function is used to calculate the mean -median-mode-in-python-without-libraries/">median of a group, that is, the 50th percentile of the data. If the passed argument is empty, StatisticsError is raised.

 ` # Python code to demonstrate how it works ` ` # mean -median-mode-in-python-without-libraries/">median_grouped () `   ` # import statistics for statistical processing ` ` import ` ` statistics `   ` # initializing list ` ` li ` ` = ` ` [` ` 1 ` `, ` ` 2 ` `, ` ` 2 ` `, ` ` 3 ` `, ` ` 3 ` `, ` ` 3 ` `] `   ` # using mean -median-mode-in-python-without-libraries/">median_grouped () to calculate the 50th percentile ` ` print ` ` (` ` "The 50th percentile of data is:" ` `, end ` ` = ` ` "") ` ` print ` ` (statistics.mean -median-mode-in-python-without-libraries/">median_grouped (li)) `

Output:

` The 50th percentile of data is: 2.5 `

This article courtesy of Manjit Singh . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

## Shop

Learn programming in R: courses

\$

Best Python online courses for 2022

\$

Best laptop for Fortnite

\$

Best laptop for Excel

\$

Best laptop for Solidworks

\$

Best laptop for Roblox

\$

Best computer for crypto mining

\$

Best laptop for Sims 4

\$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

NUMPYNUMPY

How to specify multiple return types using type-hints

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

NUMPYNUMPY

glob exclude pattern

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

NUMPYNUMPY

Python CSV error: line contains NULL byte

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

## Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries