Write Python. Understand every error.

Open the editor

Python programs · NumPy

Python program to find the mean, median and standard deviation with NumPy

NumPy summarises a list of numbers in one line each. This is the usual way to analyse marks, temperatures or prices.

Approach

  1. Put the numbers in a NumPy array.
  2. Call .mean(), np.median() and .std().
  3. Round the standard deviation for a tidy result.

The program

import numpy as np

marks = np.array([72, 85, 90, 64, 78])
print("Mean:", marks.mean())
print("Median:", np.median(marks))
print("Std:", round(marks.std(), 2))

Sample run

Output
Mean: 77.8
Median: 78.0
Std: 9.22

Run this program in Py-CRAFT

How the code works

The mean is the sum divided by the count, (72 + 85 + 90 + 64 + 78) ÷ 5 = 77.8. The median is the middle value after sorting, which is 78. The standard deviation measures how far the marks spread from the mean; NumPy's .std() divides by the number of values (the population version). round(..., 2) keeps two decimals.

Try changing it

Common mistakes

Learn the ideas

More programs

Share this page: