NumPy is the foundational library for numerical computing in Python, providing efficient multi-dimensional array objects (ndarray) and a vast collection of mathematical functions that operate on arrays. Built on optimized C and Fortran libraries (BLAS/LAPACK), NumPy enables vectorized operations that eliminate Python loops and execute at near-native speed. Understanding broadcasting rules, memory layout, and array views versus copies is essential β these concepts allow you to write code that is not only fast but also memory-efficient, forming the backbone of scientific Python workflows from data analysis to deep learning.
What This Cheat Sheet Covers
This topic spans 23 focused tables and 219 indexed concepts. Below is a complete table-by-table outline of this topic, spanning foundational concepts through advanced details.
Table 1: Array Creation β Initialization and Construction
| Method | Example | Description |
|---|---|---|
arr = np.array([1, 2, 3]) | Create array from Python list or tuple; most common creation method for known data. | |
np.zeros((3, 4)) | Initialize array filled with zeros; specify shape as tuple. | |
np.ones((2, 3), dtype=int) | Create array filled with ones; optional dtype specification. | |
np.empty((2, 2)) | Allocate uninitialized array; fastest creation but contains arbitrary values. | |
np.full((3, 3), 7) | Fill array with specified constant value. | |
np.arange(0, 10, 2) | Generate evenly spaced values within interval; works like Python range() but returns ndarray. |