Python Program to Calculate Standard Deviation

In this python tutorial, you will learn how to Calculate Standard Deviation with built in functions of the python programming language.

How to Calculate Standard Deviation?

Standard deviation is the measure of dispersion of a set of data from its mean. Standard Deviation is calculated by :

where x1, x2, x3, xn are observed values in sample data, \scriptstyle {\overline {x}} is the mean value of observations and N is the number of sample observations

Let’s take a look at the source code,  here the values are assigned in the code, the built in functions carry out the function.

# Python Program to Calculate Standard Deviation

import statistics

sample = [1, 2, 3, 4, 5]

print("Standard Deviation of sample is % s "
                % (statistics.stdev(sample)))


Standard Deviation of sample is 1.5811388300841898
  1. At the start we use the import statistics where the import keyword is used to import the values which the variable  statistics holds
  2. Next, we create a simple set of data containing integers [1, 2, 3, 4, 5] and assign the values to the variable sample  respectively.
  3. We declare a print function we display the statement ("Standard Deviation of sample is % s " % (statistics.stdev(sample))), where the in the place of the variable s, the standard deviation of the values in the variable sample will be displayed.


  • The %s operator lets you add a value into a Python string
  •  The stdev() is a built in function which can be used to calculate the standard deviation.
  • The print statement is followed by a period, to initiate the format function.
  • The print statement/string to be displayed in enclosed in double quotes.