w3resource logo
Python Exercises

Python NumPy: Create a new array of 3*5, filled with 2

Python NumPy: Array Object Exercise-40 with Solution

Write a Python program to create a new array of 3*5, filled with 2.

Sample Solution:-

Python Code:

import numpy as np
#using no.full
x = np.full((3, 5), 2, dtype=np.uint)
print(x)
#using no.ones
y = np.ones([3, 5], dtype=np.uint) *2
print(y)

Sample Output:

[[2 2 2 2 2]                                                           
 [2 2 2 2 2]                                                           
 [2 2 2 2 2]]                                                          
[[2 2 2 2 2]                                                           
 [2 2 2 2 2]                                                           
 [2 2 2 2 2]]

Python Code Editor:

import numpy as np
#using no.full
x = np.full((3, 5), 2, dtype=np.uint)
print(x)
#using no.ones
y = np.ones([3, 5], dtype=np.uint) *2
print(y)

Improve this sample solution and post your code through Disqus