Skip to main content
NumPy beginner Lesson 2 of 12

Introduction to NumPy

Learn why NumPy exists, how it compares to plain Python lists, and set up your first numerical computing environment.

What Is NumPy?

NumPy (Numerical Python) is the foundational library for scientific computing in Python. It provides a high-performance multidimensional array object (ndarray) and a large collection of mathematical functions that operate on those arrays without Python-level loops.

Every major data science library — Pandas, Scikit-Learn, PyTorch, TensorFlow — stores and moves data as NumPy arrays or compatible objects. Learning NumPy is learning the lingua franca of numerical Python.

Why Not Just Use Python Lists?

Python lists are flexible: they can hold objects of any type and grow dynamically. That flexibility comes at a cost. Each element in a Python list is a full Python object with a type tag, reference count, and pointer to its value — roughly 28 bytes for a single integer.

A NumPy array of 1 million float64 values uses 8 MB. The equivalent Python list uses roughly 35 MB just for the object overhead, before counting the actual values.

More importantly, NumPy operations compile down to tight C loops. A Python for loop that adds two lists element-by-element interprets bytecode on every iteration. NumPy’s vectorized addition executes a single C function call that processes all elements in one pass.

import numpy as np
import time

size = 10_000_000

# Python list approach
py_list = list(range(size))
start = time.perf_counter()
result = [x * 2 for x in py_list]
py_time = time.perf_counter() - start

# NumPy approach
np_array = np.arange(size)
start = time.perf_counter()
result = np_array * 2
np_time = time.perf_counter() - start

print(f"Python list: {py_time:.3f}s")
print(f"NumPy array: {np_time:.3f}s")
print(f"Speedup:     {py_time / np_time:.0f}x")
# Python list: 0.412s
# NumPy array: 0.008s
# Speedup:     51x

Installation

NumPy is included in most Python distributions for data work. Install it directly with pip or conda:

pip install numpy

# or with conda (includes optimized BLAS)
conda install numpy

Your First NumPy Array

import numpy as np

# Create a 1-D array from a Python list
temperatures = np.array([22.1, 23.5, 19.8, 25.0, 21.3])

print(temperatures)        # [22.1 23.5 19.8 25.  21.3]
print(temperatures.dtype)  # float64 — NumPy inferred the type
print(temperatures.shape)  # (5,) — a 1-D array with 5 elements
print(temperatures.ndim)   # 1
print(temperatures.size)   # 5 — total number of elements

# Arithmetic broadcasts across the entire array — no loop needed
celsius_to_fahrenheit = temperatures * 9 / 5 + 32
print(celsius_to_fahrenheit)  # [71.78 74.3  67.64 77.   70.34]

# Aggregations
print(f"Mean:  {temperatures.mean():.1f}")  # 22.3
print(f"Max:   {temperatures.max():.1f}")   # 25.0
print(f"Std:   {temperatures.std():.1f}")   # 1.7

Key Array Properties

Every NumPy array has three essential properties you’ll use constantly:

PropertyDescriptionExample
dtypeElement data typefloat64, int32, bool
shapeTuple of dimension sizes(3, 4) for a 3×4 matrix
ndimNumber of dimensions2 for a matrix
sizeTotal element countshape[0] * shape[1] * ...
nbytesMemory usage in bytessize * dtype.itemsize
import numpy as np

# 2-D array (matrix) — rows × columns
matrix = np.array([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
])

print(matrix.shape)   # (3, 3)
print(matrix.ndim)    # 2
print(matrix.dtype)   # int64
print(matrix.nbytes)  # 72 — 9 elements × 8 bytes each

What’s Next

The next tutorial covers all the ways to create NumPy arrays — from sequences, zeros/ones, ranges, random values, and by reading data from files.

Frequently Asked Questions

Why is NumPy faster than Python lists?
NumPy arrays store data in contiguous blocks of typed memory (like C arrays). Python lists store pointers to Python objects scattered in memory. NumPy operations run as compiled C loops; Python loops interpret bytecode on every iteration. The difference is typically 10–100x for numerical work.
When should I use NumPy instead of Pandas?
Use NumPy for homogeneous numerical arrays — linear algebra, signal processing, image data, custom ML math. Use Pandas when your data has named columns, mixed types, or you need groupby/merge/resample operations. Pandas DataFrames are built on top of NumPy arrays.
Does NumPy use multiple CPU cores?
Standard NumPy operations are single-threaded, but NumPy links against optimized BLAS/LAPACK libraries (OpenBLAS, MKL) that use multiple threads automatically for large matrix operations like np.dot and np.linalg routines.