ADVERTISEMENT
Google AdSense Slot (headerLeaderboard)

Set your Publisher ID in src/config/ads.ts to display live ads.

NumPy Tutorial• 6 min read

NumPy Introduction & ndarray Architecture

Why NumPy is 50x faster than standard Python lists: Contiguous memory and C-level loops

NumPy (Numerical Python) is the universal foundation of the Python data science stack, providing the N-dimensional array (ndarray) with compiled C-level execution speed.

Python Lists vs NumPy ndarrays

Standard Python lists store pointers to scattered objects across memory, requiring dynamic type checking and memory indirection on every element access.

NumPy ndarray stores elements in a single contiguous block of memory with a uniform data type (homogeneity), enabling SIMD CPU vectorization and cache locality.

Operations on ndarray execute in compiled C/Fortran routines, eliminating Python loop overhead.

Creating ndarrays and Inspecting Attributes
import numpy as np

# Create 2D array (matrix)
matrix = np.array([[10, 20, 30], [40, 50, 60]], dtype=np.float64)

print("Array content:
", matrix)
print("Shape (rows, cols):", matrix.shape)
print("Dimensions:", matrix.ndim)
print("Data type:", matrix.dtype)
print("Total elements:", matrix.size)
print("Memory size in bytes:", matrix.nbytes)
ndarray properties reveal shape, data type, dimension count, and memory footprint.
Pro Tip

Always specify exact dtypes like np.float32 or np.int16 when handling large datasets to cut memory consumption in half.

ADVERTISEMENT
Google AdSense Slot (inContentBanner)

Set your Publisher ID in src/config/ads.ts to display live ads.

Test Yourself With Exercise

+15 XP

Why are NumPy ndarrays significantly faster for numerical calculations than native Python lists?