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 statistics module in Python
To import the statistical module and use its mathematical functions, write the following at the start of the Python program:
1 2 3 |
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:
1 2 3 4 5 6 7 8 9 |
# 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:
1 2 3 4 |
50.833333333333336 26.716666666666665 |
median() function in Python
To calculate the median, use the median() function in Python. Let us see an example:
1 2 3 4 5 6 7 8 9 |
# 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:
1 2 3 4 |
13 7 |
mode() function in Python
To calculate the mode, use the mode() function in Python. Let us see an example:
1 2 3 4 5 6 7 8 9 |
# 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:
1 2 3 4 |
3 1 |
stdev() function in Python
To calculate the standard deviation, use the stdev() function in Python. Let us see an example:
1 2 3 4 5 6 7 8 9 |
# 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:
1 2 3 4 |
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:
No Comments