Python Practice › NumPy Programs › Program 69

Make an array read-only

EasyNumPy Programs · 69 of 100

Write a NumPy program to make an array immutable.

Advertisement
Output
Error: assignment destination is read-only

Approach

  1. Set flags.writeable = False.

Solution

Python
import numpy as npa = np.array([1, 2, 3])a.flags.writeable = Falsetry:    a[0] = 10except ValueError as e:    print("Error:", e)
Output
Error: assignment destination is read-only

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

Leave a Comment