Calculating the Standard Deviation in Python

Calculating standard deviation with Python

Càlcul de la desviació estàndard en Python

🧭 Role: Exercici
🗂️ Area: Statistics
📅 Year: 2025
🧩 Stack: Python
📝 Credits: Gloc from GalaDesa

Results / Insights

desviacio estandar mostral
idesviacio estandar poblacional

When working with data in Python, you’ll often need to measure how spread out the values are. A common way to do this is by calculating the standard deviation.

With NumPy, it’s straightforward:

import numpy as np

# Calculate the sample standard deviation
sample_std = round(np.std(observations, ddof=1), 2)

# Calculate the population standard deviation
population_std = round(np.std(observations, ddof=0), 2)

sample_std, population_std

Here, ddof stands for Delta Degrees of Freedom:

  • ddof=0 is used for the whole population.
  • ddof=1 is used for a sample (this is the unbiased estimator).

Result:
For this dataset, the sample standard deviation is 1.55, and the population standard deviation is 1.45.