# How to Calculate Standard Deviation in Python

from math import sqrt  def standard_deviation(lst, population=True):    """Calculates the standard deviation for a list of numbers."""    num_items = len(lst)    mean = sum(lst) / num_items    differences = [x - mean for x in lst]    sq_differences = [d ** 2 for d in differences]    ssd = sum(sq_differences)     # Note: it would be better to return a value and then print it outside    # the function, but this is just a quick way to print out the values along    # the way.    if population is True:        print('This is POPULATION standard deviation.')        variance = ssd / num_items    else:        print('This is SAMPLE standard deviation.')        variance = ssd / (num_items - 1)    sd = sqrt(variance)    # You could return sd here.     print('The mean of {} is {}.'.format(lst, mean))    print('The differences are {}.'.format(differences))    print('The sum of squared differences is {}.'.format(ssd))    print('The variance is {}.'.format(variance))    print('The standard deviation is {}.'.format(sd))    print('--------------------------')  s = [98, 127, 133, 147, 170, 197, 201, 211, 255]standard_deviation(s)standard_deviation(s, population=False)

Output:

This is POPULATION standard deviation.The mean of [98, 127, 133, 147, 170, 197, 201, 211, 255] is 171.0.The differences are [-73.0, -44.0, -38.0, -24.0, -1.0, 26.0, 30.0, 40.0, 84.0].The sum of squared differences is 19518.0.The variance is 2168.6666666666665.The standard deviation is 46.56894530335282.--------------------------This is SAMPLE standard deviation.The mean of [98, 127, 133, 147, 170, 197, 201, 211, 255] is 171.0.The differences are [-73.0, -44.0, -38.0, -24.0, -1.0, 26.0, 30.0, 40.0, 84.0].The sum of squared differences is 19518.0.The variance is 2439.75.The standard deviation is 49.393825525059306.--------------------------
Sun, 2016-07-17 18:42
Offline
Joined: 1 year 6 months ago

#### losing precision

I was doing my own program for calculating (sample)standard deviation and ended with similar code:

for i in lst:
aux += ((i-mean)**2)

However, this seems to lose precision, and your solution give more accurate results

differences = [x - mean for x in lst]
sq_differences = [d ** 2 for d in differences]
ssd = sum(sq_differences)

Why is that separating the operations gives different results?

Fri, 2016-08-12 21:30
Offline
Joined: 2 years 10 months ago

#### I not sure. Could you post

I not sure. Could you post your complete function? You could put a print statement in there to see what the values are. Or run them on pythontutor.com to compare.