from math import sqrt
from __future__ import division # python 2.7

Problem12: Calculating mean, variance, and standard deviation of a distribution in python:

def calculate_mvs(X, P_X):
    """input: 2 lists, [X values] and corresponding [P(X)] values.
       return: mean, variance, and standard deviation of a given distribution"""
    distributions = zip(X, P_X)
    mean = sum([x * px for x, px in distributions])
    variance = sum([ (x - mean)**2 * px for x, px in distributions ])
    std = sqrt(variance)
    return mean, variance, std
X = [4, 3, 2, 1] # grades A, B, C, D

aP_X = [0/25, 25/25, 0/25, 0/25] # grade distribution of class a for grades A, B, C, and D respectively
bP_X = [5/25, 15/25, 5/25, 0/25] # grade distribution of class b ..
cP_X = [5/25, 10/25, 5/25, 5/25] # grade distribution of class c ..

print("mean, variance, standard deviation:")
print(calculate_mvs(X, aP_X)) # out: (3.0, 0.0, 0.0)
print(calculate_mvs(X, bP_X)) # out: (2.9999999999999996, 0.4, 0.6324555320336759)
print(calculate_mvs(X, cP_X)) # out: (2.6, 1.04, 1.019803902718557)

Sample calculation for class c distribution to show how the function calculate_mvs() works.

c_mean = ((5)*(4) + (10)*(3) + (5)*(2) + (5)*(1)) / 25
c_variance = (4-2.6)**2 * (5/25) + (3-2.6)**2 * (10/25) + (2-2.6)**2 * (5/25) + (1-2.6)**2 * (5/25)
c_sdt = sqrt(c_variance)

print(c_mean, c_variance, c_sdt) # out: (2.6, 1.04, 1.019803902718557)