17 Aug 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:
- mean( )
- median( )
- mode( )
- 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]))
The output is as follows:
50.833333333333336
median() function in Python
To calculate the median, use the median() function in Python. The median is the middlemost value in an ordered dataset:
- Odd number of observations: Median is the exact middle value.
Example: For [2, 5, 7], the median is 5. - Even number of observations: Median is the average of the two middle values.
Example: For [2, 5, 7, 10], the median is (5+7)/2=6
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]))
The output is as follows:
13
mode() function in Python
To calculate the mode, use the mode() function in Python. Mode is the most frequent value in a dataset.
In statistics, the mode is the value that occurs most frequently in a dataset. It is one of the three key measures of central tendency, alongside the mean and median.
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, 5, 6, 7])) print(st.mode([1, 3, 3, 4, 5, 6, 7, 7]))
The output is as follows:
3 3
stdev() function in Python
To calculate the standard deviation, use the stdev() function in Python. Standard deviation is a key statistical measure that shows how spread out data values are around the mean. A low standard deviation means values are tightly clustered near the average, while a high standard deviation means they are widely dispersed.
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([5, 7, 9]))
The output is as follows:
7.668736780560655 2.0
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:
No Comments