What is the statistics.stdev() method in Python?

Overview

Statistics module in python contains different methods which are used to calculate mathematical and statistical analysis of numeric data. With this module, one can obtain and perform different mathematical operations involving numerical data.

There are various methods under the statistics module which are used for the different statistical analysis. Some of these methods include:

  1. mean() method: to calculate the mean value of a data set
  2. stdev() method: used to calculate the standard deviation of a data set.
  3. pstdev() method: used to calculate the population standard deviation of a given data set.
  4. median() method: used to calculate the median value of a data set
  5. mode() method: to calculate the modal value of a data set.
  6. geometric_mean method: used to calculate the geometric mean of a data set. etc.

In this shot, we are taking a closer look at the statistics.stdev() method.

The statistics.stdev() method in python returns the standard deviation of a given data sample. This method is unlike the statistics.pstdev(), which calculates the standard deviation of an entire population; instead, it only computes the standard deviation from a sample of data.

Standard deviation is simply the square root of variance or the sample variance and is given by:

standard deviation = √variance

Syntax

statistics.stdev(data, xbar)

Parameter value(s)

Parameter

Details

data

This is required argument. It is the data to be used. It can be a list, a sequence or iterator

xbar

This is optional. It is the mean of the data provided. If the value is not provided, by default, it calculates the mean of the data

Return value

statistics.stdev() method returns a float value which represents the standard deviation of a given data.

Code example

Let us use the statistics.stdev() method to calculate the standard deviation for a given data as follows:

import statistics
# creating a data set
data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# to obtain the standard deviation
standard_deviation = statistics.stdev(data)
print('The standard deviation is', standard_deviation)

Code explanation

  • Line 1: we imported the statistics module
  • Line 4: we created a data set we called data
  • Line 6: we used the statistics.stdev() method to obtain the standard deviation of the data and assigned the value to another variable standard_deviation.
  • Line 7: we printed the standard_deviation which contains the standard deviation of the given data.

Free Resources