GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • w3resource
  • Front End
  • HTML
  • CSS
  • JavaScript
  • HTML5
  • Schema.org
  • php.js
  • Twitter Bootstrap
  • Responsive Web Design tutorial
  • Zurb Foundation 3 tutorials
  • Pure CSS
  • HTML5 Canvas
  • JavaScript Course

This view

  • Original page
  • New address

GeezerVison is showing www.w3resource.com — NumPy: Make an array immutable - w3resource

NumPy: Make an array immutable

Last update on April 26 2025 12:38:54 (UTC/GMT +8 hours)


Make Array Read-Only

Write a NumPy program to make an array immutable (read-only).

Sample Solution:

Python Code:

# Importing the NumPy library and aliasing it as 'np'
import numpy as np

# Creating a NumPy array 'x' filled with zeros, containing 10 elements
x = np.zeros(10)

# Setting the 'writeable' flag of the array 'x' to False, making it read-only
x.flags.writeable = False

# Printing a message to test whether the array is read-only or not
print("Test the array is read-only or not:")

# Attempting to change the value of the first element in the array 'x'
print("Try to change the value of the first element:")
x[0] = 1  # This line will raise an error since the array is read-only 

Sample Output:

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[0] = 1                                                           
ValueError: assignment destination is read-only

Explanation:

In the above code –

‘x = np.zeros(10)’ creates a 1D NumPy array with 10 elements, all initialized to 0 and stores it in the variable ‘x’.

x.flags.writeable = False: This line sets the writeable flag of the x array to False, making the array read-only. This means that any attempt to modify the elements of the array will result in an error.

x[0] = 1: This line tries to set the first element of the array x to 1. However, since the array is read-only, this operation will raise a ValueError.


For more Practice: Solve these Related Problems:

  • Write a NumPy program to set an array's writeable flag to False using the flags.writeable attribute and test its immutability.
  • Create a function that toggles the read-only state of an array and attempts modifications to trigger an exception.
  • Verify that an array becomes immutable by catching errors on assignment attempts after setting the flag.
  • Experiment with changing an array’s writeable flag back to True and validate that modifications can then be made.

Go to:

  • NumPy Array Exercises Home ↩
  • NumPy Exercises Home ↩

PREV : Vector of Size 10 (0–1 Excluded)
NEXT : Sum Multiples of 3 or 5 Below 100


Python-Numpy Code Editor:

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.


  • Daily Coding Challenges & Projects
  • Weekly Trends and Language Statistics



You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.