Python / Data Science Essentials Interview Questions
What is NumPy and why is it significantly faster than plain Python lists for numerical work?
NumPy (Numerical Python) is the foundational library for scientific computing in Python. At its core it provides the ndarray  an N-dimensional array of a single, fixed data type stored in a contiguous block of memory. That single design decision is the source of almost all of NumPy's performance advantage over Python lists.
Python lists store references to Python objects scattered around the heap. Each arithmetic operation on a list requires Python to look up each object, check its type, extract the value, compute, and then box the result back into a new Python object. A million-element loop pays that overhead a million times.
NumPy sidesteps the overhead in two ways. First, all elements in an ndarray share the same dtype (e.g., float64, int32), so there is no per-element type check and no boxing. Second, NumPy operations are implemented as compiled C (and sometimes Fortran) routines that operate on the raw memory buffer in tight loops  this is called vectorisation. The Python interpreter is invoked once for the whole array, not once per element.
#import numpy as np import time n = 10_000_000 py_list = list(range(n)) np_arr = np.arange(n, dtype=np.float64) t0 = time.perf_counter() py_result = [x * 2.5 for x in py_list] print(f'List loop : {time.perf_counter()-t0:.3f}s') t0 = time.perf_counter() np_result = np_arr * 2.5 # vectorised  no Python loop print(f'NumPy : {time.perf_counter()-t0:.3f}s') # Typical ratio: 50xÂ200x faster for NumPy
Memory is also more compact. A Python integer object takes ~28 bytes; a NumPy int64 element takes exactly 8 bytes. For a million-element array that is the difference between 28 MB and 8 MB.
More Related questions...