Tsallaka zuwa abun ciki
Justice Tech School

Assigning Values

Slices and masks can be assigned directly. Values broadcast.

Example
a = np.zeros(6, dtype=int)
a[1:3] = 5
a[a == 0] = -1
print(a)
b = np.arange(6).reshape(2, 3)
b[:, 1] = [10, 20]
print(b)

Result

[-1  5  5 -1 -1 -1]
[[ 0 10  2]
 [ 3 20  5]]

Exercise

Set a slice.

a[1:3]  5

Quiz Test

Does assignment broadcast?

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