Back to Python
2026-05-065 min read

Standard Deviation (Python Programming)

Learn Standard Deviation (Python Programming) step by step with clear examples and exercises.

Why This Matters

Standard deviation is a crucial concept in data analysis that helps us understand the dispersion of data points from the mean (average) value. By learning how to calculate standard deviation using Python, you will be able to work effectively with datasets and gain insights into real-world scenarios such as quality control, financial analysis, statistical modeling, and machine learning.

Prerequisites

To fully grasp this lesson, you should have a good understanding of the following concepts:

  1. Basic Python programming (variables, functions, loops, lists)
  2. Understanding of arithmetic mean and median
  3. Familiarity with Python libraries such as NumPy and pandas
  4. Knowledge of statistical concepts like variance and skewness

Core Concept

Definition

Standard deviation measures the average distance between data points and the mean value in a dataset. It is calculated using the following formula:

σ = sqrt( ( Σ (x - μ)² ) / n )

Where:

  • x represents each individual data point,
  • μ is the arithmetic mean of the dataset,
  • Σ denotes summation, and
  • n is the number of data points in the dataset.

Calculating Standard Deviation in Python

To calculate standard deviation in Python, we can use either built-in functions or popular libraries like NumPy and pandas. Here's an example using both methods:

import statistics
import numpy as np

data = [2, 4, 6, 8, 10]
mean = statistics.mean(data)
std_dev_statistics = statistics.stdev(data)
std_dev_numpy = np.std(data)

print("Mean:", mean)
print("Standard Deviation (Statistics):", std_dev_statistics)
print("Standard Deviation (Numpy):", std_dev_numpy)

In this example, we first import the statistics and NumPy modules and create a list of data points. We then calculate the mean and standard deviation using both built-in functions mean() and stdev() from the statistics module, as well as the std() function from the NumPy library.

Variance vs Standard Deviation

Variance is the average squared distance from the mean, while standard deviation is the square root of variance. Standard deviation provides a measure of dispersion in terms of the original data units, making it easier to interpret than variance.

Worked Example

Let's work through an example to better understand how to use Python to calculate standard deviation:

import statistics
import numpy as np

data = [12, 14, 15, 16, 18, 19, 20, 23, 26, 30]
mean = statistics.mean(data)
std_dev_statistics = statistics.stdev(data)
std_dev_numpy = np.std(data)

print("Mean:", mean)
print("Standard Deviation (Statistics):", std_dev_statistics)
print("Standard Deviation (Numpy):", std_dev_numpy)

In this example, we have a dataset containing 10 numbers representing different measurements. We calculate the mean and standard deviation using both built-in functions from the statistics module and the std() function from the NumPy library. The output will be:

Mean: 18.6
Standard Deviation (Statistics): 4.358899072794568
Standard Deviation (Numpy): 4.358899072794568

Common Mistakes

1. Calculating Standard Deviation Manually

Manually calculating standard deviation can be error-prone and time-consuming, especially for large datasets. It is recommended to use built-in functions or libraries like NumPy and pandas to perform these calculations efficiently.

2. Misinterpreting the Results

Standard deviation provides a measure of dispersion, but it does not indicate whether the data is normally distributed. Always be mindful of the context in which you are working and consider other statistical measures like skewness or kurtosis to further analyze your dataset.

3. Assuming Standard Deviation Applies Only to Numbers

Standard deviation can also be calculated for datasets containing non-numeric data, such as text strings or categorical variables. However, this requires converting the data into a numerical format using techniques like one-hot encoding or TF-IDF (Term Frequency-Inverse Document Frequency).

4. Calculating Standard Deviation with Outliers

Outliers can significantly affect the standard deviation calculation, leading to inflated or deflated values. It is essential to identify and handle outliers appropriately before calculating standard deviation, such as using robust methods like the median absolute deviation (MAD) or trimming a certain percentage of extreme values.

Practice Questions

  1. Calculate the standard deviation of the following dataset: [3, 5, 7, 9]
  2. A manufacturing company wants to analyze the quality of their products using standard deviation. They collect data on the weight of 500 items and find that the mean weight is 100 grams with a standard deviation of 5 grams. What does this tell you about the variation in weights?
  3. A researcher wants to compare the standard deviations of two different datasets containing the heights of students from two schools. They calculate the standard deviations as follows: School A - 2 inches, School B - 1 inch. Which school has a more uniform distribution of student heights?
  4. A dataset contains outliers that significantly affect the standard deviation calculation. How can you handle these outliers to get a more accurate measure of dispersion?

FAQ

Q: What is the difference between variance and standard deviation?

A: Variance is the average squared distance from the mean, while standard deviation is the square root of variance. Standard deviation provides a measure of dispersion in terms of the original data units, making it easier to interpret than variance.

Q: How can I calculate standard deviation using Python without using built-in functions or libraries?

A: It is possible to implement the formula for calculating standard deviation manually in Python, but it can be quite complex and error-prone compared to using built-in functions or libraries like NumPy and pandas. It is recommended to use the built-in functions whenever possible.

Q: Can I calculate standard deviation for a dataset containing negative and positive numbers?

A: Yes, when calculating standard deviation for a dataset with both positive and negative numbers, you should first find the absolute value of each number before squaring and summing them up. After that, divide by n and take the square root as usual.

Q: Can I calculate standard deviation using Python without knowing the total number of data points (n)?

A: No, it is essential to know the total number of data points (n) when calculating standard deviation in order to properly normalize the variance calculation. If you have a dataset where the total number of data points is not known, consider using libraries like NumPy or pandas that can handle this automatically.

Standard Deviation (Python Programming) | Python | XQA Learn