Skip to content
Learn Netverks

Lesson

Step 9/36 25% through track

indexing-slicing

Indexing and slicing

Last reviewed May 28, 2026 Content v20260528
Track mode
server_script
Means
Server runner
Reading
~1 min
Level
beginner

This lesson

This lesson teaches Indexing and slicing: NumPy ndarray operations, vectorization, and numerical patterns used across the Python scientific stack.

Teams apply Indexing and slicing in every serious NumPy project—skipping it leaves blind spots in analysis and reviews.

You will apply Indexing and slicing in contexts like: Notebooks, feature engineering pipelines, and custom numerical code.

Read the narrative, run `import numpy as np` snippets in the playground (install NumPy with pip if the runner lacks it), tweak shapes and dtypes, and complete MCQs.

When you can explain the previous lesson's ideas in your own words.

NumPy indexing extends Python slicing to multiple dimensions. Slices return views when possible—mutating a slice can change the original array.

1D indexing

import numpy as np
a = np.array([10, 20, 30, 40, 50])
print(a[0], a[-1], a[1:4])

2D indexing

  • arr[row, col] — single element
  • arr[row, :] — entire row
  • arr[:, col] — entire column
  • arr[0:2, 1:3] — submatrix slice

Views vs copies

Basic slicing usually creates a view sharing memory. Fancy indexing (integer arrays) returns a copy. Use .copy() when you need independence.

Important interview questions and answers

  1. Q: a[1:4] inclusive?
    A: Stop index exclusive—elements at indices 1, 2, 3.
  2. Q: Why care about views?
    A: In-place edits to slices affect parent array—subtle bugs in pipelines.

Self-check

  1. Extract the second column of a 2D array.
  2. Does slicing always copy?

Pitfall: Slicing returns views—mutations affect the parent array.

Interview prep

View?

Basic slices share memory—mutations affect parent.

2D index?

arr[row, col]—comma separates axes.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Slice is view?
  • Negative index?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump