NUMPY BASIC EXERCISE 13


Create a 5×5 array of zeros, ones and 4s

In this exercise, we are going to create a 5×5 array of zeros, ones and 4s

Code

import numpy as np
arr = np.zeros((5, 5))
print("5x5 array of zeros")
print(arr)
arr = np.ones((5, 5))
print("5x5 array of ones")
print(arr)
arr = np.full((5, 5), 4)
print("5x5 array of 4s")
print(arr)

Output

5x5 array of zeros
[[0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0.]]
5x5 array of ones
[[1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1.]]
5x5 array of 4s
[[4 4 4 4 4]
 [4 4 4 4 4]
 [4 4 4 4 4]
 [4 4 4 4 4]
 [4 4 4 4 4]]

Code Editor

import numpy as np arr = np.zeros((5, 5)) print("5x5 array of zeros") print(arr) arr = np.ones((5, 5)) print("5x5 array of ones") print(arr) arr = np.full((5, 5), 4) print("5x5 array of 4s") print(arr)