How to use NumPy Array Slicing?
How to use NumPy Array Slicing, explain with example.
Ravi Vishwakarma is a dedicated Software Developer with a passion for crafting efficient and innovative solutions. With a keen eye for detail and years of experience, he excels in developing robust software systems that meet client needs. His expertise spans across multiple programming languages and technologies, making him a valuable asset in any software development project.
Anubhav Kumar
17-Nov-2025Syntax:
1. Slicing a 1-D Array
Example:
a) Basic slicing
Output:
b) Without start
Output:
c) Without end
Output:
d) With step
Output:
e) Reverse slicing
Output:
2. Slicing a 2-D Array
a) Slice specific row
Output:
b) Slice specific column
Output:
c) Slice a sub-matrix
Output:
This extracts rows 0–1 and columns 1–2.
3. Slicing With Steps (2D)
Example: Every 2nd row and column
Output:
4. Slicing Higher-Dimensional Arrays (3D)
If you have a 3D array (like an image):
Get 1st "layer":
Get middle column of every layer:
Summary Cheatsheet
a[start:end]a[1:4]a[:end]a[:3]a[start:]a[2:]a[::step]a[::2]a[::-1]a[row, col]a[1,2]a[rows, cols]a[0:2, 1:3]