CHAPTER 05
Beginner
NumPy Indexing and Slicing
Updated: May 18, 2026
5 min read
# CHAPTER 5
NumPy Indexing and Slicing
1. Chapter Introduction
Accessing specific elements, rows, columns, or sub-arrays from NumPy arrays is fundamental to data manipulation. NumPy provides several powerful indexing mechanisms, each suited for different access patterns.2. Basic Indexing
python
3. Slicing
python
4. Boolean Indexing (Most Important!)
python
5. Fancy Indexing
python
6. Views vs Copies
python
7. Common Mistakes
-
View vs copy: Slices are views — modifying them modifies the original. Use
.copy()when you want an independent copy.
-
Boolean indexing with OR:
salaries > 60000 or salaries < 40000fails — use|(notor) for element-wise OR.
8. MCQs
Question 1
arr[-1] accesses?
Question 2
arr[2:6] includes index?
Question 3
arr[::-1] does?
Question 4
Boolean indexing uses?
Question 5
& in NumPy boolean conditions means?
Question 6
Array slice returns?
Question 7
.copy() creates?
Question 8
matrix[:, 2] selects?
Question 9
Fancy indexing uses?
Question 10
~mask in NumPy?
9. Interview Questions
- Q: What is the difference between a NumPy view and a copy?
- Q: How do you select elements meeting multiple conditions in NumPy?
10. Summary
NumPy indexing covers every data access pattern: integer indexing, slicing withstart:stop:step, boolean filtering with masks, and fancy indexing with index lists. Critical insight: slices are views — modify with caution, use .copy() for independence.