w3resource

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


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.


For more Practice: Solve these Related Problems:

  • Create a 10-element zero vector and update its sixth element based on a computed function.
  • Generate a null vector, then conditionally update the sixth element if a certain threshold is met.
  • Modify the sixth element of a zero vector and verify that the overall sum of the vector equals that element.
  • Create a zero vector and change the sixth element, then assert the vector’s datatype remains unchanged.

Go to:


PREV : Create 3x3 Matrix (2 to 10)
NEXT : Array from 12 to 38


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.