Google News
logo
Python Program to Change the value of an array element
In the following example of Python program to change the value of an element in an array :
Program :
import array as arr

# Creating an array of integers
my_array = arr.array('i', [10, 20, 30, 40, 50])

# Displaying the original array
print("Original array:")
for i in my_array:
    print(i, end=" ")
print()

# Changing the value of an element
my_array[2] = 35

# Displaying the modified array
print("Modified array:")
for i in my_array:
    print(i, end=" ")
print()
Output :
Original array:
10 20 30 40 50 
Modified array:
10 20 35 40 50 
In the above program, we first import the array module and create an array of integers using the `array()` constructor. We then display the original array using a `for` loop.

Next, we change the value of the element at index 2 to 35 using the index operator `[]`. Finally, we display the modified array using another `for` loop.