NumPy: Make an array immutable
NumPy: Array Object Exercise-67 with Solution
Write a NumPy program to make an array immutable (read-only).
import numpy as np x = np.zeros(10) x.flags.writeable = False print("Test the array is read-only or not:") print("Try to change the value of the first element:") x = 1
Test the array is read-only or not: Try to change the value of the first element: Traceback (most recent call last): File "24222080-0d5f-11e7-93ad-ebc5bbb63894.py", line 6, in
x = 1 ValueError: assignment destination is read-only
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a NumPy program to create a vector of size 10 with values ranging from 0 to 1, both excluded.
Next: Write a NumPy program (using numpy) to sum of all the multiples of 3 or 5 below 100.
What is the difficulty level of this exercise?
Test your Python skills with w3resource's quiz
Python: Tips of the Day
>>> m = [x ** 2 for x in range(5)] >>> m [0, 1, 4, 9, 16]
- New Content published on w3resource:
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework