Python Program to Calculate the Variance

Python Program to Calculate the Variance

Python program to find variance; In this tutorial, you will learn how to find or calculate the variance in python using numpy.

Python Program to Calculate the Variance

  • Write a program to calculate variance in python.
  • Python calculate variance without numpy

Write a program to calculate variance in python

import numpy as np

dataset= [21, 11, 19, 18, 29, 46, 20]

variance= np.var(dataset)




Note:- Python variance() is an inbuilt function that is used to calculate the variance from the sample of data (sample is a subset of populated data). Python statistics module provides potent tools, which can be used to compute anything related to Statistics.

Python calculate variance without numpy

#define a function, to calculate variance
def variance(X):
    mean = sum(X)/len(X)
    tot = 0.0
    for x in X:
        tot = tot + (x - mean)**2
    return tot/len(X)

# call the function with data set
x = [1, 2, 3, 4, 5, 6, 7, 8, 9] 
print("variance is: ", variance(x))

y = [1, 2, 3, -4, -5, -6, -7, -8] 
print("variance is: ", variance(y))

z = [10, -20, 30, -40, 50, -60, 70, -80] 
print("variance is: ", variance(z))


variance is:  6.666666666666667 
variance is:  16.5 
variance is:  2525.0 

Recommended Python Programs

  1. Python Program to Add Two Numbers
  2. Python Program to Find/Calculate Sum of n Numbers
  3. Python Program to Find/Calculate Average of 3, 4, 5…n numbers
  4. Python Program to Print ASCII Value of Character
  5. Write a Program to Calculate Simple Interest in Python
  6. Python Program to Compute Compound Interest
  7. Leap Year Program in Python
  8. Python Program to Print Star Pattern
  9. Number Pattern Programs in Python
  10. Python Program to Print Even and Odd numbers From 1 to N
  11. Python Abs() Function: For Absolute Value
  12. How to Check Whether a Number is Fibonacci or Not in Python
  13. Python: Program to Find Power of Number
  14. Python Program to Reverse a Numbers
  15. Python Program to Find Smallest/Minimum of n Numbers
  16. Python Program to Find Largest/Maximum of n Numbers
  17. Python Program to Find The Net Bill Amount After Discount
  18. Python Program to Print Numbers From N to 1 and 1 to N
  19. Python Program to Print Numbers Divisible by 3, 5, 7
  20. Python Program to Print Prime Number 1 to N
  21. How to Find Square of Number in Python
  22. Python Program to Calculate Cube of Number
  23. Python Program to Find LCM of Two Numbers
  24. BMI (Body Mass Index) Calculator in Python
  25. Palindrome Program in Python using while loop, Function, etc
  26. Python: Program to Count Total Number of Bits in Number
  27. Python Random Number Generator Code
  28. Python Program to Calculate n-th term of a Fibonacci Series
  29. Zip Zap Zoom Python Program
  30. Python: program to convert Celsius to Fahrenheit
  31. Python Program to Swap Two Numbers
  32. Python Program to Convert Meters into Yards, Yards into Meters
  33. Python Program to Convert Kilometers to Meters, Miles
  34. Python Program to Find Perfect Number
  35. Python: Program to Find Strong Number
  36. Python Program Create Basic Calculator
  37. Python Program For math.floor() Method
  38. Python Program to Find Sum of Series 1/1! 2/2! 3/3! …1/n!
  39. Python: Program to Convert Decimal to Binary, Octal and Hexadecimal
  40. Python Program to Find Roots of Quadratic Equation
  41. Python Program to Print Alphabets from A to Z in Uppercase and Lowercase
  42. Python Program to Check Given Input is Alphabet, Number or Special Character
  43. Python Program to Check IF a Number is Power of Another Number
  44. Python Check Binary Representation of Given Number is Palindrome or Not
  45. Python Program to Draw a Pie Chart
  46. Python Program Input the Radius of Circle and Compute the Area
  47. Python Program to Calculate the Area of a Rectangle
  48. Python Program to Calculate Area of Triangle
  49. Python Program to Find Area and Circumference of Circle using Radius
  50. Python Program that Accepts Marks in 5 Subjects and Outputs Average Marks
  51. Python Program to Print Binary Value of Numbers From 1 to N


My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *