NumPy Getting Started

This lesson walks through installing NumPy with pip and importing it into a script using the standard np alias.

Installing NumPy with pip

NumPy is not part of the Python standard library, so it must be installed separately. The simplest way is with pip, Python's package installer, which downloads NumPy from the Python Package Index (PyPI) and installs it into your current Python environment.

Install NumPy

pip install numpy

Importing NumPy

Once installed, bring NumPy into a script with a single import statement. By convention, almost every developer imports it under the short alias np, which keeps code readable and matches the official documentation and most tutorials.

Import NumPy and Check the Version

import numpy as np

print(np.__version__)
Note: If Python raises ModuleNotFoundError: No module named 'numpy', the install went into a different Python environment than the one you're running. Re-run pip install numpy using the same interpreter, or activate the correct virtual environment first.

Trying Out Your Installation

A quick way to confirm everything works end-to-end is to build a small array, inspect it, and run some math on it.

First NumPy Script

import numpy as np

arr = np.array([1, 2, 3, 4, 5])
print(arr)          # [1 2 3 4 5]
print(arr.shape)    # (5,)

doubled = arr + arr
print(doubled)      # [ 2  4  6  8 10]

Basic NumPy Math Functions

import numpy as np

values = np.array([4, 9, 16, 25])

print(np.sqrt(values))   # [2. 3. 4. 5.]
print(np.mean(values))   # 13.5
CommandPurpose
pip install numpyInstalls the latest NumPy release
pip install numpy==1.26.4Installs a specific NumPy version
pip install --upgrade numpyUpgrades an existing install to the latest version
pip show numpyDisplays the installed version and file location

Exercise: NumPy Getting Started

What is the standard convention for importing NumPy in Python code?