Python statistics.stdev() Function
The Python statistics.stdev() function calculates the standard deviation from a sample of data.
In statistics, the standard deviation is a measure of spread. It quantifies the variation of data values. This function is very much similar to variance, but, variance provides the spread value.
A low measure of Standard Deviation indicates that the data is less spread out, whereas high values are vice versa. The mathematical representation of Standard Deviation is as follows −

Syntax
Following is the basic syntax for the statistics.stdev() function −
statistics.stdev([data-set], xbar)
Parameters
- data - set: These values are used as any sequence, list and iterator.
- xbar: This is the optional mean of the data-set.
Return Value
This function returns the actual standard deviation of the given values i.e., passed as a parameter.
Example 1
In the below example, we are creating the standard deviation of the given data-set using the statistics.stdev() function.
import statistics
x = [2, 4, 6, 8, 10]
y = statistics.stdev(x)
print("Standard Deviation of the sample is %s " % x)
Output
The result is produced as follows −
Standard Deviation of the sample is [2, 4, 6, 8, 10]
Example 2
Now, we are demonstrating Standard Deviation by importing fractions using statistics.stdev() function.
from statistics import stdev
from fractions import Fraction as fr
x = (1, 2, 3, 4, 5)
y = (-3, -4, -9, -3, -2)
z = (2.2, 1.23, 3.54, 0.23, 4.5)
print("The Standard Deviation of x is % s" %(stdev(x)))
print("The Standard Deviation of y is % s" %(stdev(y)))
print("The Standard Deviation of z is % s" %(stdev(z)))
Output
This will produce the following result −
The Standard Deviation of x is 1.5811388300841898 The Standard Deviation of y is 2.7748873851023217 The Standard Deviation of z is 1.7182403789924157
Example 3
Here, we are finding the difference between the result of variance and Standard Deviation using statistics.stdev() function.
import statistics
x = [2, 4, 5, 6, 7]
print("Standard Deviation of the sample is % s" %(statistics.stdev(x)))
print("Variance of the sample is % s" %(statistics.stdev(x)))
Output
The output is obtained as follows −
Standard Deviation of the sample is 1.9235384061671346 Variance of the sample is 1.9235384061671346
Example 4
In the following example we are utilizing the xbar parameter using the statistics.stdev() function.
import statistics
x = (1, 2.3, 4.05, 1.9, 2.2)
y = statistics.mean(x)
print("Standard Deviation of sample is % s" %(statistics.stdev(x, xbar = y)))
Output
We will get the following output as follows −
Standard Deviation of sample is 1.1092790451459902
Example 5
The following example elaborates on a StaticError using statistics.stdev()function.
import statistics x = [4] print(statistics.stdev(x))
Output
This produces the following output −
Traceback (most recent call last):
File "/home/cg/root/67654/main.py", line 3, in <module>
print(statistics.stdev(x))
File "/usr/lib/python3.10/statistics.py", line 828, in stdev
var = variance(data, xbar)
File "/usr/lib/python3.10/statistics.py", line 767, in variance
raise StatisticsError('variance requires at least two data points')
statistics.StatisticsError: variance requires at least two data points
python_modules.htm