Q:

Write a NumPy program to create a new array of given shape (5,6) and type, filled with zero

0

Write a NumPy program to create a new array of given shape (5,6) and type, filled with zeros.
Change the said array in the following format:

Given array:
[[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 0 0 0 0 0]]
New array:
[[3 0 3 0 3 0]
[7 0 7 0 7 0]
[3 0 3 0 3 0]
[7 0 7 0 7 0]
[3 0 3 0 3 0]]

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

import numpy as np
nums = np.zeros(shape=(5, 6), dtype='int')
print("Original array:")
print(nums)
nums[::2, ::2] = 3
nums[1::2, ::2] = 7
print("\nNew array:")
print(nums)

Sample Output:

Original array:
[[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 0 0 0 0 0]]

New array:
[[3 0 3 0 3 0]
 [7 0 7 0 7 0]
 [3 0 3 0 3 0]
 [7 0 7 0 7 0]
 [3 0 3 0 3 0]]

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now