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.
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.
a.shapegives the size as (rows, columns);a.reshape(r, c)rearranges the same values into a new shape.a[r, c]is one element;a[r]a row;a[:, c]a column; slices work as for lists.- Filtering:
a[a > 4]gives the elements meeting a condition.
Whole-array arithmetic and statistics
Operators work element by element: a * 2, a + b, a > 4. No loop is needed.
sum,mean,average,std(standard deviation),var(variance): with no axis they use every element;axis=0works down each column;axis=1along each row.- PLS warning: the PLS says the default axis for
meanandsumis 0, but in real NumPy the default is no axis at all, soa.sum()adds every element (29 below) whilea.sum(axis=0)gives the column totals. Always write the axis you mean.
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
np.round(a, n),np.sort(a),np.transpose(a)(rows become columns).np.where(condition, x, y)picks from x where the condition is true, otherwise from y.np.vstackstacks arrays as rows;np.column_stackputs 1D arrays side by side as columns..tolist()converts back to an ordinary list.
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.
- ```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()) ```
- 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.
Check yourself
- 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`
- 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]`
- 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`.
- 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]`
- 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]]`
- 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']`
- 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]`
- 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.
Sources
- Pearson Edexcel International Advanced Subsidiary/Advanced Level in Computer Science specification, Issue 1 (March 2026)
- Pearson Edexcel IAL Computer Science Unit 4 Programming Language Subset (PLS)
- Pearson Edexcel IAL Computer Science sample assessment materials (Unit 3 and Unit 4)
Last updated 8 November 2026. Spotted a mistake? Tell us.