Home » Python » Python programs

# Python program to find the variance

**Variance in python**: Here, we are going to learn **how to find the variance of given data set using python program?**

Submitted by Anuj Singh, on June 30, 2019

While dealing with a large data, how many samples do we need to look at before we can have justified confidence in our answer? This depends on the variance of the dataset.

**Variance** tells us about the divergence and the inconsistency of the sample. So in this python article, we are going to build a function.

Mathematically we define it as:

So the following function can be used while working on a program with big data which is very useful and help you a lot.

So here is the code:

def variance(X): mean = sum(X)/len(X) tot = 0.0 for x in X: tot = tot + (x - mean)**2 return tot/len(X) # main code # a simple data-set sample = [1, 2, 3, 4, 5] print("variance of the sample is: ", variance(sample)) sample = [1, 2, 3, -4, -5] print("variance of the sample is: ", variance(sample)) sample = [10, -20, 30, -40, 50] print("variance of the sample is: ", variance(sample))

**Output:**

ariance of the sample is: 2.0 variance of the sample is: 10.64 variance of the sample is: 1064.0

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions

**Ad:**
Are you a blogger? Join our Blogging forum.

Learn PCB Designing: PCB DESIGNING TUTORIAL