NumPy (Numerical Python) is the standard library for numerical computing in Python. It provides multi-dimensional arrays and a large set of functions that operate on them efficiently. Most data-science and scientific libraries—Pandas, Matplotlib, scikit-learn—build on NumPy. This guide covers installation, creating arrays from lists, basic element-wise operations, and why NumPy is preferred over plain lists for numbers. You should know basic Python and data types.

1. When to use NumPy

Use NumPy when you have numeric data (vectors, matrices, or higher-dimensional arrays) and you want fast operations, compact storage, or APIs that expect ndarray. For general collections of mixed types or when you need to append frequently, Python lists are fine. For tabular data with column names and missing values, Pandas is usually better. NumPy is the layer underneath: Pandas DataFrames have a .values attribute that returns a NumPy array.

2. Install and import

NumPy is not in the standard library. Install it in your environment (preferably a virtual environment):

pip install numpy

If you get ModuleNotFoundError: No module named 'numpy', ensure you installed it for the same Python interpreter you are using. Import with the conventional alias:

import numpy as np

3. Create arrays from lists

np.array(...) builds an array from a list or nested lists. The result is an ndarray with a fixed type (e.g. int64, float64) and shape. One-dimensional lists become 1D arrays; lists of lists become 2D arrays (matrices).

import numpy as np

arr = np.array([1, 2, 3, 4, 5])
print(arr)         # [1 2 3 4 5]
print(type(arr))   # numpy.ndarray
print(arr.dtype)   # int64 (or int32 depending on platform)

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

4. Element-wise operations

Arithmetic operators (+, -, *, /, **) apply element-wise when both operands are arrays of the same shape, or when one is an array and the other a scalar. This is much faster than looping in Python and is the main reason to use NumPy for numerical code.

arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
print(arr1 + arr2)   # [5 7 9]
print(arr1 * 2)      # [2 4 6]
print(arr1 ** 2)     # [1 4 9]

5. Useful constructors and functions

np.zeros(shape), np.ones(shape), and np.arange(start, stop, step) create arrays without building a Python list. np.linspace(start, stop, num) gives evenly spaced values. For random numbers use np.random (e.g. np.random.randn(3, 3) for a 3×3 normal sample). See NumPy Arrays for more constructors and Array Operations for reductions (sum, mean, etc.).

print(np.zeros(5))           # [0. 0. 0. 0. 0.]
print(np.arange(0, 10, 2))   # [0 2 4 6 8]
print(np.linspace(0, 1, 5)) # [0.   0.25 0.5  0.75 1.  ]

6. Why NumPy is faster and more compact

NumPy arrays store elements in a contiguous block of memory with a fixed type, so operations can be implemented in C and avoid per-element Python overhead. Lists store pointers to Python objects, so they are flexible but slower and use more memory for numbers. For large arrays or tight loops, prefer NumPy; for small ad-hoc lists, plain lists are fine.

7. Next steps

Go to NumPy Arrays for shape, dtype, and more ways to create arrays; Indexing and Slicing for selecting elements and sub-arrays; and Array Operations for aggregations and broadcasting. For tabular data see Pandas and the How-To parse CSV with Pandas. When you see TypeError or shape mismatches, check array shapes with .shape and the Errors section.