Python provides a Statistics module which has useful functions like mean( ), mode( ) and median( ) and so on. The following example programs demonstrates the working of mean( ),mode( ) and median( ) functions.

#Program

import statistics
x=[1,6,3,7,5,2,4]
print("Mean of 'x' is : " ,(statistics.mean(x)))
y=[4,1,9,3,1,8,1,7,1,32,1,56,84,5,1,-33]
print("Mode of 'y' is : " ,(statistics.mode(y)))
z=[2,4,-6,9,3,8,1,2,7,1]
print("Median of 'z' is : " ,(statistics.median(z)))

Output

Mean of ‘x’ is : 4.0

Mode of ‘y’ is : 1

Median of ‘z’ is : 2.5

We can also write programs to demonstrate mean( ), mode( ) and median( ) on different range of values.

# To demonstrate the mean( ) on different range of values.

from statistics import mean
from fractions import Fraction as fr
#Positive integers
a = (9, 3, 1, 5, 7, 2)
#Floating point numbers
b = (4.8, 5.1, 6.7, 18.9)
#Fractional numbers
c = (fr(3, 4), fr(14, 52), fr(70, 13), fr(12, 7))
#Negative integers
d = (-4, -2, -15, -29, -32)
#Both positive and negative integers
e = (-9, -8, -7, -6, 6, 7, 8, 9)
print("Mean of 'a' is : " ,(mean(a)))
print("Mean of 'b' is : " ,(mean(b)))
print("Mean of 'c' is : " ,(mean(c)))
print("Mean of 'd' is : " ,(mean(d)))
print("Mean of 'e' is : " ,(mean(e)))

Output

Mean of ‘a’ is : 4.5

Mean of ‘b’ is : 8.875

Mean of ‘c’ is : 2955/1456

Mean of ‘d’ is : -16.4

Mean of ‘e’ is : 0.0

# To demonstrate the mode( ) on different range of values.

from statistics import mode
from fractions import Fraction as fr
#Positive integers
a = (9, 3, 3, 11, 5, 7, 2)
#Floating point numbers
b = (4.8, 5.1, 6.7, 4.8, 18.9)
#Fractional numbers
c = (fr(3, 4), fr(14, 52), fr(70, 13), fr(12, 7),fr(14,52))
#Negative integers
d = (-4, -2, -15, -29, -32,-4)
#Both positive and negative integers
e = (-9, -8, -7, -6, 6, 7, -7, 8, 9)
print("Mode of 'a' is : " ,(mode(a)))
print("Mode of 'b' is : " ,(mode(b)))
print("Mode of 'c' is : " ,(mode(c)))
print("Mode of 'd' is : " ,(mode(d)))
print("Mode of 'e' is : " ,(mode(e)))

Output

Mode of ‘a’ is : 3

Mode of ‘b’ is : 4.8

Mode of ‘c’ is : 7/26

Mode of ‘d’ is : -4

Mode of ‘e’ is : -7

# To demonstrate the median( ) on different range of values.

from statistics import median
from fractions import Fraction as fr
#Positive integers
a = (9, 3, 11, 5, 7, 2)
#Floating point numbers
b = (4.8, 5.1, 6.7, 18.9)
#Fractional numbers
c = (fr(3, 4), fr(14, 52), fr(70, 13), fr(12, 7))
#Negative integers
d = (-4, -2, -15, -29, -32)
#Both positive and negative integers
e = (-9, -8, -7, -6, 6, 7, 8, 9)
print("Median of 'a' is : " ,(median(a)))
print("Median of 'b' is : " ,(median(b)))
print("Median of 'c' is : " ,(median(c)))
print("Median of 'd' is : " ,(median(d)))
print("Median of 'e' is : " ,(median(e)))