Why NumPy Instead of Python Lists?
Consider adding 5 to every element in a list of one million numbers.- Homogeneous storage — all elements share the same type, so the array is a single contiguous memory block with no per-element Python overhead.
- C-speed operations — arithmetic is executed by precompiled C code, bypassing the Python interpreter loop.
- Vectorization — you write
array + 5instead of a loop; the looping happens inside the C layer invisibly.
Installation and Import
Creating Arrays
From a Python List
Zeros, Ones, and Empty
Always wrap multi-dimensional shapes in a tuple:
np.zeros((2, 3)), not np.zeros(2, 3). The second argument to these functions is dtype, not the second dimension.Ranges and Sequences
Random Arrays
Array Properties
Indexing and Slicing
1D Arrays
2D Arrays
Boolean (Conditional) Filtering
Array Operations
NumPy applies arithmetic operators element-wise on arrays of the same shape.Broadcasting
Broadcasting lets you apply an operation between arrays of different shapes without making actual copies. NumPy “virtually expands” the smaller array to match the larger one — following two rules:- Dimensions are compared from right to left.
- Each pair must be equal or one of them must be 1.
(2, 3) + (2,) raises a ValueError because the rightmost dimensions (3 vs 2) are neither equal nor 1.
Aggregate Functions
Reshaping and Flattening
reshape(3, 4) works on a 12-element array, but reshape(5, 3) would raise a ValueError.
Sorting
Practical Example: Normalize Student Scores
Normalization rescales values to a 0–1 range. It is a standard preprocessing step before feeding data into machine learning models.Next: Pandas
Build on NumPy to work with labeled, tabular data using DataFrames — the primary data structure for data analysis in Python.
NumPy documentation
The official NumPy reference documents every function, with examples. Bookmark it — you will use it often.