0
0
PythonProgramBeginner · 2 min read

Python Program to Find Standard Deviation

You can find the standard deviation in Python by calculating the square root of the average of squared differences from the mean using import math and code like std_dev = math.sqrt(sum((x - mean) ** 2 for x in data) / n).
📋

Examples

Input[2, 4, 4, 4, 5, 5, 7, 9]
Output2.0
Input[10, 12, 23, 23, 16, 23, 21, 16]
Output5.237229365663817
Input[5]
Output0.0
🧠

How to Think About It

To find the standard deviation, first find the average (mean) of the numbers. Then, for each number, find how far it is from the mean and square that difference. Next, find the average of those squared differences. Finally, take the square root of that average to get the standard deviation.
📐

Algorithm

1
Get the list of numbers as input
2
Calculate the mean (average) of the numbers
3
For each number, subtract the mean and square the result
4
Calculate the average of these squared differences
5
Take the square root of this average to get the standard deviation
6
Return or print the standard deviation
💻

Code

python
import math

def standard_deviation(data):
    n = len(data)
    mean = sum(data) / n
    variance = sum((x - mean) ** 2 for x in data) / n
    return math.sqrt(variance)

# Example usage
numbers = [2, 4, 4, 4, 5, 5, 7, 9]
print(standard_deviation(numbers))
Output
2.0
🔍

Dry Run

Let's trace the example [2, 4, 4, 4, 5, 5, 7, 9] through the code

1

Calculate length and mean

n = 8, mean = (2+4+4+4+5+5+7+9)/8 = 5.0

2

Calculate squared differences

[(2-5)^2=9, (4-5)^2=1, (4-5)^2=1, (4-5)^2=1, (5-5)^2=0, (5-5)^2=0, (7-5)^2=4, (9-5)^2=16]

3

Calculate variance

variance = (9+1+1+1+0+0+4+16)/8 = 4.0

4

Calculate standard deviation

std_dev = sqrt(4.0) = 2.0

NumberDifference from MeanSquared Difference
22 - 5 = -39
44 - 5 = -11
44 - 5 = -11
44 - 5 = -11
55 - 5 = 00
55 - 5 = 00
77 - 5 = 24
99 - 5 = 416
💡

Why This Works

Step 1: Calculate the mean

The mean is the average value, found by adding all numbers and dividing by the count using sum(data) / n.

Step 2: Find squared differences

Each number's difference from the mean is squared to remove negative signs and emphasize larger differences using (x - mean) ** 2.

Step 3: Calculate variance and standard deviation

Variance is the average of squared differences, and standard deviation is the square root of variance using math.sqrt(variance) to return to original units.

🔄

Alternative Approaches

Using statistics module
python
import statistics
numbers = [2, 4, 4, 4, 5, 5, 7, 9]
print(statistics.pstdev(numbers))
This is the simplest and most readable way but requires Python 3.4+ and does not show calculation steps.
Using numpy library
python
import numpy as np
numbers = np.array([2, 4, 4, 4, 5, 5, 7, 9])
print(np.std(numbers))
Fast and efficient for large data but requires installing numpy.

Complexity: O(n) time, O(1) space

Time Complexity

The program loops through the data twice: once to calculate the mean and once to calculate squared differences, so it runs in linear time O(n).

Space Complexity

The program uses a fixed amount of extra space regardless of input size, so space complexity is O(1).

Which Approach is Fastest?

Using the built-in statistics.pstdev() or numpy's np.std() is fastest for large data due to optimized C implementations.

ApproachTimeSpaceBest For
Manual calculationO(n)O(1)Learning and small data
statistics.pstdev()O(n)O(1)Simple, built-in, small to medium data
numpy np.std()O(n)O(1)Large data and performance
💡
Use Python's built-in statistics.pstdev() for quick standard deviation calculation on population data.
⚠️
Beginners often forget to square the differences or take the square root at the end, leading to incorrect results.