/Curriculum/Core NumPyData Access

09. NumPy Slicing & ViewsBeginner

Zero-copy memory views with arr[start:stop:step], row slicing, and array reversing.

15 mins

Concept Overview

Slicing extracts sub-arrays using `start:stop:step` notation. In NumPy, basic slicing creates a zero-copy memory 'view' pointing directly to the original array.

Hardware Mental Model

A slice is a window or frame placed over an existing canvas. It doesn't paint a new picture; it just looks at a portion of the original artwork.

Key Concepts (1)Click snippet to load in editor

NumPy slices do NOT copy data into new memory. They create views with adjusted stride offsets.

sub = arr[1:4]

⚠ Common Mistakes & Pitfalls

Avoid these frequent beginner syntax and logic traps
❌ Incorrect:sub = arr[1:4]; sub[0] = 999 # Accidental mutation of original array!
✓ Correct:sub = arr[1:4].copy(); sub[0] = 999

Use `.copy()` if you want an independent duplicate that will not affect the original array.

Pro Tip: Remember: Slices are views. Use `.copy()` when data isolation is required.

📌 Quick Revision

Core takeaway points from this topic
`arr[start:stop:step]`: start is inclusive, stop is exclusive.
`arr[::-1]`: Reverses the array.
`matrix[0:2, 1:3]`: Extracts a 2x2 submatrix.
Basic slices are zero-copy memory views.
Editor: 09. NumPy Slicing & Views
Python ● Ready
Initializing Python IDE...
Ctrl+Enter
Execution Result
Click "RUN CODE" to execute and inspect array state

🎯 Try It Yourself: Practice Challenge

Hands-on Mode

Solve the objective below using NumPy vectorized syntax

Objective:Reverse a NumPy Array

Create `arr = np.array([1, 2, 3, 4, 5])`, reverse it using slice `[::-1]`, and print the result.

Expected Output Target:[5 4 3 2 1]
Practice Workspace
Python ● Ready
Initializing Python IDE...
Ctrl+Enter
Execution Result
Click "RUN CODE" to execute and inspect array state