UrbanPro
true

Learn Python Training from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

A Python Program to calculate Variance and Standard Deviation

Ashish K Sharma
28/06/2022 0 0

                            Finding the variance and the standard deviation.

Variance and Standard Deviation are related measures of dispersion that tell us more about a list of numbers,like are they all similar, clustered near the mean or are they all different. To calculate either of these, we first need to find the difference of each of the numbers from the mean. The variance is the average of the square of those differences. The variance is calculated using the formula:

 

                        Variance = (Σ(xi - xmean) ^ 2) / n.

 

Where, xi = individual numbers

              xmean = mean of these numbers

and         n = number of values in the list.

 

So,now, in order to calculate the variance, for each value in the list, we take the difference between that number and the mean and square it. Then we add all those squared differences together and, finally, divide the whole sum by n to find the variance.

And to calculate the standard deviation as well, all we have to do is to calculate the square root of the variance.

 

 

 

Now, we will write a Python program to find out the variance and the standard deviation of a list of numbers.

 

                                  PYTHON PROGRAM.

                  

import os

import sys

 

def calculate_mean(numbers):

    s = sum(numbers)

    N = len(numbers)

    mean = s/N

    return mean

 

 

def find_differences(numbers):

    mean  = calculate_mean(numbers)

    diff = []

 

    for num in numbers:

        diff.append(num-mean)

 

    return diff

 def calculate_variance(numbers):

    diff = find_differences(numbers)

    squared_diff = []

   

    for d in diff:

        squared_diff.append(d**2)

 

    # Find the variance

    sum_squared_diff = sum(squared_diff)

   

    variance = sum_squared_diff/len(numbers)

    return variance

 

donations = [100,60,70,900,100,200,500,500,503,600,1000,1200]

variance = calculate_variance(donations)

std = variance ** 0.5

print("The standard deviations of the list of numbers is {0}".format(std))

 

 

                                         Program anaylsis

The function calculate_variance() calculates the variance of the list of numbers passed to it. Firstly, it calls the find_differences() function to calculate the difference of each of the numbers from the mean and appends each difference so found to a list. We notice that the mean is calculated by another function, calculate_mean(),called from the find_differences() function. Then, using the sum function, it sums up the differences so found. Then, the variance is calculated using the formula, dividing the sum of the differences by the total numbers in the list of numbers.

 

                                      OUTPUT OF THE PROGRAM.

>>>

The standard deviations of the list of numbers is 375.5627166887931

>>> 

 

0 Dislike
Follow 1

Please Enter a comment

Submit

Other Lessons for You

Bigdata hadoop training institute in pune
BigData What is BigData Characterstics of BigData Problems with BigData Handling BigData • Distributed Systems Introduction to Distributed Systems Problems with Existing Distributed...

Python Dictionary
Python dictionary is an unordered collection of items. While other compound datatypes have only value as an element, a dictionary has a key: value pair. Dictionaries are optimized to retrieve values when...

Using Lambda and Filter with Regular expressions.
1) Start the Python interpreter and import the re module: >>> import re 2)Define a tuple strings to filter with regular expressions: t=('python','pycon','perl','peril','step','stop','come','came','exercise','hello','storm','my.mail','year','yam') ...

Write your first Python program in 10 minutes
1. Download python from python official site search "python download" in google 2. Install in your machine 3. verify using : "python --version" command 4. Write first program using notepad create...

Python Puzzle: Find The Checksum To Ensure Flawless Data Transfer
Find below the puzzle: sampleInput='''5195 753 2468''' The first item's(5195) largest and smallest values are 9 and 1, and their difference is 8. The second item's(753) largest and smallest values...
X

Looking for Python Training Classes?

The best tutors for Python Training Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn Python Training with the Best Tutors

The best Tutors for Python Training Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more