Skip to content
Justice Tech School

Changing Values

Assign to an index, slice or mask to update many elements at once.

Example
import numpy as np
a = np.arange(6)
a[0] = 100
a[1:3] = 0
a[a > 3] = -1
print(a)

Result

[-1  0  0  3 -1 -1]

Exercise

Set masked values.

a[a > 3]  -1

Quiz Test

Can you assign to a slice?

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