Skip to main content
When you process numerical data in Python — scores, prices, sensor readings, pixel values — plain Python lists work, but they are painfully slow at scale. NumPy (Numerical Python) solves this by giving you N-dimensional arrays that store homogeneous data in contiguous memory and execute math through compiled C code. The result: operations on millions of values run in milliseconds instead of seconds. This page teaches you everything you need to use NumPy confidently in data analysis and AI projects.

Why NumPy Instead of Python Lists?

Consider adding 5 to every element in a list of one million numbers.
The NumPy version is roughly 100× faster because:
  1. Homogeneous storage — all elements share the same type, so the array is a single contiguous memory block with no per-element Python overhead.
  2. C-speed operations — arithmetic is executed by precompiled C code, bypassing the Python interpreter loop.
  3. Vectorization — you write array + 5 instead 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:
  1. Dimensions are compared from right to left.
  2. Each pair must be equal or one of them must be 1.
Broadcasting fails when the shapes are incompatible — for example (2, 3) + (2,) raises a ValueError because the rightmost dimensions (3 vs 2) are neither equal nor 1.

Aggregate Functions

Reshaping and Flattening

The total number of elements must stay the same: 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.
NumPy is the foundation that Pandas, Matplotlib, Seaborn, Scikit-learn, and TensorFlow are all built on. The indexing patterns, broadcasting rules, and aggregate functions you learn here apply everywhere in the Python data ecosystem.

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.