find the standard deviation of a list of numbers in python

To find the standard deviation of a list of numbers in Python, you can use the statistics module or the math module.

Here's an example using the statistics module:

main.py
import statistics

data = [1, 2, 3, 4, 5]

# calculate the standard deviation
stdev = statistics.stdev(data)

print("Standard Deviation:", stdev)
146 chars
9 lines

Output:

main.py
Standard Deviation: 1.5811388300841898
39 chars
2 lines

And here's an example using the math module:

main.py
import math

data = [1, 2, 3, 4, 5]

# first calculate the mean
mean = sum(data) / len(data)

# then calculate the variance
variance = sum([((x - mean) ** 2) for x in data]) / len(data)

# finally, calculate the standard deviation
stdev = math.sqrt(variance)

print("Standard Deviation:", stdev)
296 chars
15 lines

Output:

main.py
Standard Deviation: 1.5811388300841898
39 chars
2 lines

gistlibby LogSnag