01-Nov-2022
.
Admin
Hi Dev,
In this quick example, let's see Python Program Calculate the standard deviation. you can understand a concept of Calculate Standard Deviation in Python. We will look at example of How to Calculate the Standard Deviation of a List in Python. if you have question about standard deviation in python without inbuilt function then I will give simple example with solution. Alright, let’s dive into the steps.
Python program find standard deviation; In this tutorial, you will learn how to find standard deviation in python with and without inbuilt function.
so let's see following examples with output:
Example 1: Python program to calculate standard deviation
# import statistics library
import statistics
print(statistics.stdev([1,2,3,4,5,5,5,6]))
print(statistics.stdev([40,45,35,10,15,18], 40))
Output:
1.7268882005337975
20.292855885754474
Example 2: 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
a = [1, 2, 3, 4, 5, 6, 7]
print("Standard Deviation is: ", stdv(a))
b = [1, 2, 3, -4, -5, -6, -7]
print("Standard Deviation is: ", stdv(b))
c = [10, -20, 30, -40, 50, 60, -70]
print("Standard Deviation is: ", stdv(c))
Output:
Standard Deviation is: 2.0
Standard Deviation is: 3.843892584878203
Standard Deviation is: 44.62999814803803
I hope it can help you...
#Python