Here's a quick script for calculating standard deviation in Python without downloading external libraries. It was an exercise while working on the R page in the wiki. Please leave a comment below, if you can suggest improvements.

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. --------------------------

## 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?

## 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.