Learn NumPy
NumPy is a Python library built around the ndarray, a fast, memory-efficient array structure that replaces plain Python lists for serious numerical work.
What Is NumPy?
NumPy, short for Numerical Python, is an open-source library that adds support for large, multi-dimensional arrays and matrices, along with a huge collection of mathematical functions to operate on them efficiently. It is the foundation that most of the scientific Python ecosystem - including pandas, SciPy, scikit-learn, and TensorFlow - is built on top of.
The ndarray: NumPy's Core Object
At the heart of NumPy is the ndarray (n-dimensional array). Unlike a Python list, which stores references to scattered objects, an ndarray stores its data in one contiguous block of memory, and every element must share the same data type.
- Homogeneous - every element has the same data type (e.g. all int64 or all float64)
- Fixed size - the total number of elements is set when the array is created
- Multi-dimensional - a single ndarray can represent a vector, a matrix, or an n-dimensional tensor
- Contiguous memory - elements are packed together rather than scattered like list references
- Vectorized - mathematical operations apply to the whole array at once, without an explicit Python loop
Why Is NumPy Faster Than Python Lists?
A Python list stores pointers to individual Python objects, each carrying its own type information and reference count. Every time you loop over a list, Python has to check each element's type and unbox it before doing any math. An ndarray skips all of that: because every element is the same known type packed into a single memory block, NumPy can hand the whole array to precompiled, low-level C loops (and even CPU vector instructions) that process it in one shot instead of one Python bytecode step at a time.
List vs Array
import numpy as np
py_list = [1, 2, 3, 4]
np_array = np.array([1, 2, 3, 4])
print(type(py_list)) # <class 'list'>
print(type(np_array)) # <class 'numpy.ndarray'>
print(np_array.dtype) # int64Vectorized Operations
import numpy as np
numbers = np.array([1, 2, 3, 4, 5])
# Every element is squared in one step - no loop required
squared = numbers ** 2
print(squared) # [ 1 4 9 16 25]Measuring the Speed Difference
import numpy as np
import time
numbers = list(range(1_000_000))
np_numbers = np.array(numbers)
start = time.time()
total = sum(numbers)
print('Python list sum time:', time.time() - start)
start = time.time()
total = np_numbers.sum()
print('NumPy array sum time:', time.time() - start)Exercise: NumPy Introduction
What is the primary advantage of NumPy arrays over standard Python lists for numerical work?
Frequently Asked Questions
- What is NumPy used for?
- Fast numerical computing in Python. It provides an array type that stores data in a contiguous block and runs operations in compiled code, which makes maths on large datasets far faster than Python lists.
- Why is NumPy faster than Python lists?
- A Python list holds pointers to separate objects; a NumPy array holds raw values of one type side by side in memory. Operations run in compiled C over the whole block rather than looping in Python.
- Do I need NumPy before pandas?
- It helps. pandas is built on NumPy, so indexing, slicing and vectorised operations carry straight over, and NumPy types appear throughout pandas error messages.