How to Calculate Variance of Python NumPy Arrays?

To calculate the variance of a Python NumPy array “x”, simply use the function “np.var(x)”. Here is an example:

import numpy as np

# Goals in five matches
goals_croatia = np.array(
goals_france = np.array(

c = np.var(goals_croatia)
f = np.var(goals_france)

What is the output of this puzzle?
*Intermediate Level* (solution below)

Numpy is a popular Python library for data science focusing on arrays, vectors, and matrices.

This puzzle introduces a new feature of the numpy library: the variance function. When applied to a 1D numpy array, this function returns the variance of the array values. The variance is the average squared deviation from the mean of the values in the array.

In the puzzle, the variance of the goals of the last five games of Croatia is 0.96 and of France is 0.24. But you do not need to know the exact values to see that the variance of goals shot by Croatia is larger.

Are you a master coder?
Test your skills now!

Related Video



Leave a Comment

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