How to use NumPy Array Slicing, explain with example.
How to use NumPy Array Slicing? ICSM Computer 2210 09 Nov 2025 How to use NumPy Array Slicing, explain with example.
Syntax:
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]