What is the difference between indexing and slicing in NumPy?

Quick Overview

Compare indexing and slicing on NumPy arrays: what each operation returns, how the shape of the result changes, and when the result shares memory with the original. Tests practical understanding of views, copies and advanced indexing for data science work in Python.

What is the difference between indexing and slicing in NumPy?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

What is the difference between indexing and slicing in NumPy? ```hint Look at the result For each operation, check three things about what comes back: its type, its shape, and whether it shares memory with the original array. ``` ### What a Strong Answer Covers - What integer indexing and `start:stop:step` slicing return on 1-D and multi-dimensional arrays, and how each one changes the shape. - Which results are views that share memory with the source and which are copies, and what happens when you write to each. - How advanced indexing with integer arrays or boolean masks differs from basic indexing. - Edge behavior: negative indices, steps, and out-of-range positions or bounds. ### Follow-up Questions - How do you check whether two arrays share memory, and when should you call `.copy()` explicitly? - Why does `a[a > 0] = 0` change `a` when `a[a > 0]` on its own returns a copy? - How can a small slice keep a very large array in memory, and how do you avoid it?

Overview: Compare indexing and slicing on NumPy arrays: what each operation returns, how the shape of the result changes, and when the result shares memory with the original. Tests practical understanding of views, copies and advanced indexing for data science work in Python.

PracHub logo
General
Nov 24, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

What is the difference between indexing and slicing in NumPy?

What a Strong Answer Covers Guidance

  • What integer indexing and start:stop:step slicing return on 1-D and multi-dimensional arrays, and how each one changes the shape.
  • Which results are views that share memory with the source and which are copies, and what happens when you write to each.
  • How advanced indexing with integer arrays or boolean masks differs from basic indexing.
  • Edge behavior: negative indices, steps, and out-of-range positions or bounds.

Follow-up Questions Guidance

  • How do you check whether two arrays share memory, and when should you call .copy() explicitly?
  • Why does a[a > 0] = 0 change a when a[a > 0] on its own returns a copy?
  • How can a small slice keep a very large array in memory, and how do you avoid it?