Skip to content
Justice Tech School

Slicing

start:stop:step selects ranges. The stop is excluded. Slice each axis separately with commas.

Example
import numpy as np
a = np.arange(10)
print(a[2:5], a[::3], a[::-1][:3])
m = np.arange(12).reshape(3, 4)
print(m[1:, :2])

Result

[2 3 4] [0 3 6 9] [9 8 7]
[[4 5]
 [8 9]]

Exercise

Every second element.

a[::]

Quiz Test

Is the stop index included?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.