Python Program to Find Standard Deviation

Python program to calculate standard deviation; Through this tutorial, i am going to show you how to find or calculate standard deviation in python.

In this tutorial, i will write a program to compute standard deviation in python.

Python Program to Find Standard Deviation

See the following python programs to standard deviation; as shown below:

  • Write a program to calculate standard deviation in python.
  • How to find standard deviation in python without inbuilt function.

Write a program to calculate standard deviation in python

# import statistics library
import statistics
 
print(statistics.stdev([1,2,3,4,5,5,5,6,7,8]))
 
print(statistics.stdev([40,45,35,10,15,18,12,16], 45))

Output

2.1705094128132942
13.829962297231946

Note:- stdev() function in python is the Standard statistics Library of Python Programming Language. The use of this function is to calculate the standard deviation of given continuous numeric data.

How to find standard deviation in python without inbuilt function

#define a function, to calculate standard deviation
def stdv(X):
    mean = sum(X)/len(X)
    tot = 0.0
    for x in X:
        tot = tot + (x - mean)**2
    return (tot/len(X))**0.5
# call function with following data set
x = [1, 2, 3, 4, 5, 6, 7, 8] 
print("Standard Deviation is: ", stdv(x))
y = [1, 2, 3, -4, -5, -6, -7, -8] 
print("Standard Deviation is: ", stdv(y))
z = [10, -20, 30, -40, 50, 60, -70, 80] 
print("Standard Deviation is: ", stdv(z))

Output

Standard Deviation is:  2.29128784747792
Standard Deviation is:  4.06201920231798
Standard Deviation is:  48.925964476952316

Recommended Python Tutorials

Leave a Comment