0PricingLogin
Learn AI with Python · Lesson

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])   # 50

Slicing 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

  1. Array Creation and Properties
  2. Indexing, Slicing, and Fancy Indexing
  3. Broadcasting and Vectorized Operations
  4. Linear Algebra with NumPy
← Back to Learn AI with Python