0Pricing
Python Academy · Lesson

Indexing, Slicing, and Fancy Indexing

Access and modify array elements with advanced indexing techniques.

Basic Indexing

Access elements with zero-based integer indices. Negative indices count from the end. Multi-dimensional arrays use comma-separated indices.

import numpy as np

arr = np.array([[1,2,3],[4,5,6],[7,8,9]])
print(arr[0, 2])    # 3
print(arr[-1, -1])  # 9
print(arr[1])       # [4 5 6]

Slicing

Slicing syntax start:stop:step returns a view. Applies independently on each dimension.

import numpy as np

m = np.arange(16).reshape(4,4)
print(m[1:3, 1:3])
# [[5 6]
#  [9 10]]
print(m[::2, ::2])   # every other row and column

All lessons in this course

  1. NumPy Arrays and dtypes
  2. Array Operations and Broadcasting
  3. Indexing, Slicing, and Fancy Indexing
  4. Linear Algebra with NumPy
← Back to Python Academy