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 numpyImporting 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__)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.5Exercise: NumPy Getting Started
What is the standard convention for importing NumPy in Python code?