Python statistics module

The statistics module in Python is used to perform statistical tasks, with statistical functions, including mean, median, mode, stdev(), etc.

How to import the statistics module in Python

To import the statistical module and use its mathematical functions, write the following at the start of the Python program:

import statistics

Examples – statistics module functions

Let us see the following examples of the statistics module:

  1. mean( )
  2. median( )
  3. mode( )
  4. stdev( )

mean() function in Python

To calculate the mean, use the mean() function in Python. Let us see an example:

Demo182.py

# mean() function in Python statistics module
# Code by studyopedia

import statistics as st

print(st.mean([10, 25, 35, 60, 80, 95]))
print(st.mean([10, -16, 35, -45, 80.6, 95.7]))

The output is as follows:

50.833333333333336
26.716666666666665

median() function in Python

To calculate the median, use the median() function in Python. Let us see an example:

Demo183.py

# median() function in Python statistics module
# Code by studyopedia

import statistics as st

print(st.median([5, 10, 13, 87, 98]))
print(st.median([-34, 7, 9.8, -5, 87]))

The output is as follows:

13
7

mode() function in Python

To calculate the mode, use the mode() function in Python. Let us see an example:

Demo184.py

# mode() function in Python statistics module
# Code by studyopedia

import statistics as st

print(st.mode([1, 3, 3, 4, 7, 7, 9]))
print(st.mode([1, 5, -8, 9, 15, 19, -20]))

The output is as follows:

3
1

stdev() function in Python

To calculate the standard deviation, use the stdev() function in Python. Let us see an example:

Demo185.py

# stdev() function Python statistics module
# Code by studyopedia

import statistics as st

print(st.stdev([1, 5, 9, 11, 12, 15, 25]))
print(st.stdev([1, 4.5, -9, 10.5, -12, 23, 76]))

The output is as follows:

7.668736780560656
29.99781738092036

Python Tutorial (English)

If you liked the tutorial, spread the word and share the link and our website, Studyopedia, with others.


For Videos, Join Our YouTube Channel: Join Now


Read More:

Python Modules
Python math module
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment