Python Practice › NumPy Programs › Program 69
Make an array read-only
Write a NumPy program to make an array immutable.
Output
Error: assignment destination is read-only
Approach
- Set
flags.writeable = False.
Solution
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.