Indexing, Slicing, and Fancy Indexing
Basic slicing, boolean masking, fancy indexing, np.where() for conditional selection.
Indexing Basics
NumPy arrays use zero-based indexing like Python lists. Negative indices count from the end.
import numpy as np
a = np.array([10, 20, 30, 40, 50])
print(a[0]) # 10
print(a[-1]) # 50Slicing 1D
Slices use start:stop:step and return a view (not a copy), so modifying a slice changes the original.
print(a[1:4]) # [20 30 40]
print(a[::2]) # [10 30 50]
view = a[1:3]
view[0] = 99
print(a) # [10 99 30 40 50]All lessons in this course
- Array Creation and Properties
- Indexing, Slicing, and Fancy Indexing
- Broadcasting and Vectorized Operations
- Linear Algebra with NumPy