VOOZH about

URL: https://www.geeksforgeeks.org/python/python-statistics-stdev/

⇱ stdev() method in Python statistics module - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

stdev() method in Python statistics module

Last Updated : 11 Jul, 2025

The stdev() function in Python's statistics module is used to calculate the standard deviation of a dataset. It helps to measure the spread or variation of values in a sample. Standard deviation (SD) measures the spread of data points around the mean. A low SD indicates data points are close to the mean, while a high SD shows they are spread out. Unlike variance, SD is in the same units as the data, making it easier to interpret.

👁 formula
Standard deviation formula

Where:

  • x1,x2,x3,.......,xN are the individual data points
  • is the mean of the data
  • N is the number of data points in the sample

Example:


Output
1.5811388300841898

Syntax of stdev() method

statistics.stdev(data, xbar=None)

Parameters:

  • data: The dataset (list, tuple, etc.) of real numbers.
  • xbar (optional): The pre-calculated mean if not given, Python computes it.

Returns: The standard deviation of the values in the dataset.

Examples of stdev() method

Example 1: In this example, we calculate the standard deviation for four datasets to measure the spread, including integers, floating-point numbers and negative values.


Output
3.9761191895520196
1.8708286933869707
7.8182478855559445
0.41967844833872525

Example 2: In this example, we calculate the standard deviation and variance for a dataset to measure the spread of values.


Output
1.5811388300841898
2.5

Example 3: In this example, we calculate the standard deviation by providing a precomputed mean using the xbar parameter in the stdev() function to avoid recalculating the mean.


Output
0.6047037842337906

Example 4: In this example, we attempt to calculate the standard deviation of a dataset with a single data point. Since stdev() requires at least two points, it raises a StatisticsError, which is handled using a try-except block.

Output

Error: stdev requires at least two data points
Comment
Article Tags: