w3resource

NumPy: Create a null vector of size 10 and update sixth value to 11

NumPy: Array Object Exercise-4 with Solution

Null Vector (10) & Update Sixth Value

Write a NumPy program to create a null vector of size 10 and update the sixth value to 11.

Python NumPy: Create a null vector of size 10 and update sixth value to 11

Sample Solution:

Python Code:

# Importing the NumPy library with an alias 'np'
import numpy as np

# Creating a NumPy array 'x' filled with zeros of length 10
x = np.zeros(10)

# Printing the initial array 'x' filled with zeros
print(x)

# Modifying the sixth value (index 6, considering zero-based indexing) of the array to 11
print("Update sixth value to 11")
x[6] = 11

# Printing the updated array 'x' after modifying the sixth value
print(x)

Sample Output:

[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]                               
Update sixth value to 11                                                
[  0.   0.   0.   0.   0.   0.  11.   0.   0.   0.]

Explanation:

In the above code -

np.zeros(10): Creates a one-dimensional NumPy array of length 10, with all elements initialized to 0.

x[6] = 11: Sets the 7th element of the NumPy array (at index 6, since Python uses zero-based indexing) to the value 11.

print(x): Prints the modified NumPy array.

Python-Numpy Code Editor:

Previous: Write a NumPy program to create a 3x3 matrix with values ranging from 2 to 10.
Next: Write a NumPy program to create a array with values ranging from 12 to 38.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/python-exercises/numpy/python-numpy-exercise-4.php