What is NumPy?
NumPy (Numerical Python) is a free Python library (a package of ready-made code) for working with numbers in bulk. Its main object is the ndarray (n-dimensional array).
Install once: pip install numpy. Use it in a program: import numpy as np (np is the usual short name).
An array is a set of items of the same type, stored side by side in memory, reached by index from 0.
Creating a 1-D array from a list
import numpy as np L = [10, 20, 30, 40] a = np.array(L) print(a) # [10 20 30 40] (no commas) print(a[0], a[-1]) # 10 40
You can pass a list directly: np.array([1.5, 2, 3]), or a tuple.
Creating a 2-D array from a list of lists
b = np.array([[1, 2, 3],
[4, 5, 6]])
print(b[1, 2]) # 6 (row 1, column 2)Each inner list becomes one row. All inner lists must have the same length, or NumPy cannot make a proper grid.
Array attributes: ndim, shape, size, dtype
a.ndim: number of dimensions (1 for a row, 2 for a table).a.shape: size in each dimension, as a tuple. (4,) for 4 items; (2, 3) for 2 rows ร 3 columns.a.size: total number of items (2 ร 3 = 6).a.dtype: type of the items (int64, float64, <Uโฆ).a.itemsize: bytes per item.
One type only: upcasting and dtype
If the list mixes types, NumPy changes all items to one common type: int + float โ float; anything + str โ str. You can also choose: np.array([1, 2, 3], dtype=float) โ [1. 2. 3.].
List vs NumPy array
- List: mixed types allowed; array: one type.
- List:
[1, 2] * 2โ [1, 2, 1, 2]; array:np.array([1, 2]) * 2โ [2 4] (element-wise). - Array: uses less memory and is much faster for big data.
- List: easy to grow with append; array: fixed size when made.
Other ways to make arrays (good to know): np.zeros(3), np.ones((2, 2)), np.arange(0, 10, 2), np.linspace(0, 1, 5).
Try it: marks as an array
Write the marks of 3 friends in 2 subjects as a list of lists, like [[70, 80], [65, 90], [88, 75]]. Make it an array. Before running, predict shape, ndim and size. Then print m + 5 and m.mean(). Check your shape idea with the sliders in step 6 of the 3D.
Key formulas and definitions
- import numpy as np; a = np.array(list)
- shape = (rows, cols); size = rows ร cols; ndim = number of dimensions
- dtype: int + float โ float; with str โ str
- array * k multiplies each item; list * k repeats the list
Worked examples
1. a = np.array([5, 10, 15]). Give a.ndim, a.shape, a.size.
ndim = 1, shape = (3,), size = 3.
2. b = np.array([[1, 2], [3, 4], [5, 6]]). Give shape and size, and b[2, 0].
3 rows, 2 columns โ shape (3, 2), size 6. b[2, 0] = 5 (row 2, column 0).
3. What is the dtype of np.array([1, 2, 3.5])?
float64; it prints as [1. 2. 3.5].
4. Compare [2, 4] * 3 and np.array([2, 4]) * 3.
List: [2, 4, 2, 4, 2, 4] (repeated). Array: [6 12] (each item ร 3).
5. Marks m = np.array([60, 72, 85]). Add 5 grace marks to everyone and find the mean.
m + 5 = [65 77 90]; mean = (65 + 77 + 90) / 3 = 77.33.
6. A 2-D array has shape (4, 5). How many items does it have, and how many are in each row?
size = 4 ร 5 = 20 items; each row has 5 items.
Common mistakes
- Forgetting import numpy as np, then np gives NameError.
- Writing np.array(1, 2, 3) instead of np.array([1, 2, 3]).
- Expecting array * 2 to repeat like a list; it multiplies each item.
- Making a 2-D array from inner lists of different lengths.