All subjects · Computer Science · WCP04 Advanced Practical Programming and Problem-solving

Numerical data with NumPy arrays

Edexcel IAL Computer Science · WCP04 Advanced Practical Programming and Problem-solving

NumPy arrays hold numbers of one type in a grid and let you do arithmetic on a whole array at once. They are faster and shorter to use than nested lists for numerical work.

Practise this in the app: instant marking and a fix for every mistake. Free, no email needed to start.

Practise this lesson

Creating arrays

import numpy as np, then np.array(list) makes an array from a list or a list of lists. Every element has the same type: pass a type code such as dtype="i" (integer) or dtype="f" (float) to choose it. The PLS also lists 'b' as bool and 'S' as str, but in real NumPy 'b' means a signed 8-bit integer (use dtype=bool or '?' for True/False) and 'S' means bytes (text shows as b'ab'; plain strings get a 'U' type automatically). 'M' is datetime64.

Whole-array arithmetic and statistics

Operators work element by element: a * 2, a + b, a > 4. No loop is needed.

import numpy as np
a = np.array([[3, 8, 1],
              [6, 2, 9]])
print(a.shape, a[1, 2], a[:, 1])
print(a * 2)
print(a[a > 4])
print(a.sum(), a.sum(axis=0), a.mean(axis=1))

Output:

(2, 3) 9 [8 2]
[[ 6 16  2]
 [12  4 18]]
[8 6 9]
29 [ 9 10 10] [4.         5.66666667]

Useful functions

import numpy as np
v = np.array([1.234, 5.678, 9.015])
print(np.round(v, 1), np.sort(np.array([4, 1, 3])))
a = np.arange(6).reshape(2, 3)
print(a.reshape(3, 2).tolist(), np.transpose(a).tolist())
print(np.where(a > 2, a, 0).tolist())
print(np.vstack([[1, 2], [3, 4]]).tolist(), np.column_stack([[1, 2], [3, 4]]).tolist())
f = np.array([1, 2, 3], dtype="f")
print(f.dtype, np.std(np.array([2, 4, 4, 4, 5, 5, 7, 9])))
print(np.array([1, 0, 2], dtype="b").dtype, np.array([1, 0, 2], dtype=bool).tolist(), np.array(["ab"], dtype="S"))

Output:

[1.2 5.7 9. ] [1 3 4]
[[0, 1], [2, 3], [4, 5]] [[0, 3], [1, 4], [2, 5]]
[[0, 0, 0], [3, 4, 5]]
[[1, 2], [3, 4]] [[1, 3], [2, 4]]
float32 2.0
int8 [True, False, True] [b'ab']

Loading and saving text files

np.savetxt(name, a, fmt="%d", delimiter=",") writes an array to a text file; np.loadtxt(name, delimiter=",", dtype=int) reads it back. Use skiprows=1 to skip a header line.

import numpy as np
marks = np.array([[71, 64], [88, 59]])
np.savetxt("marks.csv", marks, fmt="%d", delimiter=",")
back = np.loadtxt("marks.csv", delimiter=",", dtype=int)
print(back.tolist(), np.average(back, axis=0).tolist())

Output:

[[71, 64], [88, 59]] [79.5, 61.5]

Worked example

Five judges score two dives. Find each dive's mean score and the highest score given by each judge.

  1. ```python import numpy as np scores = np.array([[7.5, 8.0, 7.0, 8.5, 7.5], [6.0, 6.5, 7.0, 6.5, 6.0]]) print(scores.mean(axis=1).tolist()) print(scores.max(axis=0).tolist()) ```
  2. Output: ``` [7.7, 6.4] [7.5, 8.0, 7.0, 8.5, 7.5] ```

Answer: Each dive is a row, so its mean is along axis 1; each judge is a column, so the maximum per judge is down axis 0.

Key idea: np.array with a type code; shape, reshape; a[r, c], a[:, c], a[a > x]; arithmetic and comparisons work element by element; sum/mean/std/var with axis=0 (columns) or axis=1 (rows); where, sort, transpose, vstack, column_stack; savetxt and loadtxt.

Check yourself

  1. What does this program print? ```python import numpy as np a = np.array([[1, 2, 3, 4], [5, 6, 7, 8]]) print(a.shape, a[1, 3]) ```
    Show answer

    (2, 4) 8. Two rows of four, so shape is (2, 4); row 1, column 3 is 8. Output: `(2, 4) 8`

  2. What does this program print? ```python import numpy as np a = np.array([12, 5, 30, 8, 21]) print(a[a > 10].tolist()) ```
    Show answer

    [12, 30, 21]. The condition keeps 12, 30 and 21. Output: `[12, 30, 21]`

  3. What does this program print? ```python import numpy as np a = np.array([[2, 4], [6, 8], [10, 12]]) print(a.sum(axis=0)[1]) ```
    Show answer

    24. axis=0 sums down the columns: [18, 24]; element 1 is `24`.

  4. What does this program print? ```python import numpy as np a = np.array([1, 2, 3]) b = np.array([10, 20, 30]) print((a * b + 1).tolist()) ```
    Show answer

    [11, 41, 91]. Multiplication is element by element: 10, 40, 90; then add 1 to each. Output: `[11, 41, 91]`

  5. What does this program print? ```python import numpy as np print(np.array([1, 2, 3, 4, 5, 6]).reshape(3, 2).tolist()) ```
    Show answer

    [[1, 2], [3, 4], [5, 6]]. Values fill row by row into 3 rows of 2. Output: `[[1, 2], [3, 4], [5, 6]]`

  6. What does this program print? ```python import numpy as np t = np.array([18, 25, 31, 22]) print(np.where(t >= 25, "hot", "ok").tolist()) ```
    Show answer

    ['ok', 'hot', 'hot', 'ok']. where picks 'hot' where t >= 25 and 'ok' elsewhere. Output: `['ok', 'hot', 'hot', 'ok']`

  7. What does this program print? ```python import numpy as np a = np.array([[1, 2], [3, 4]]) print(a.sum(), a.sum(axis=0).tolist()) ```
    Show answer

    10 [4, 6]. With no axis, real NumPy adds every element (10), whatever the PLS says about a default of 0; axis=0 gives the column totals. Output: `10 [4, 6]`

  8. Why might a programmer use a NumPy array rather than a list of lists for 10 000 temperature readings?
    • Arrays can hold values of mixed types
    • Arrays never need importing
    • Arrays can grow with append() more easily than lists
    • Arithmetic and statistics apply to the whole array at once, quickly, without writing loops
    Show answer

    Arithmetic and statistics apply to the whole array at once, quickly, without writing loops. NumPy operations are vectorised: written once for the whole array and executed efficiently.

Practise with instant feedback

Sources

Last updated 8 November 2026. Spotted a mistake? Tell us.