In NumPy, arrays can be made immutable by setting their flags to read-only. This prevents any modifications to the array’s data. Here’s how you can create an immutable NumPy array:
Python
import numpy as np
# Create a NumPy array
arr = np.array([1, 2, 3, 4, 5])
# Make the array immutable
arr.setflags(write=False)
# Try to modify the array (this will raise an error)
# arr[0] = 10 # Uncommenting this line would raise an error
# Checking if the array is immutable
print(arr.flags['WRITEABLE']) # Output: FalseOnce the array’s write flag is set to False, any attempt to modify its data will result in an error. This is useful when you want to ensure that your data remains unchanged throughout your program.
