How can you create a NumPy array filled with zeros?

How can you create a NumPy array filled with zeros?

a) By using the zeros() function
b) By using the empty() function
c) By using the ones() function
d) By using the full() function

Answer:

a) By using the zeros() function

Explanation:

You can create a NumPy array filled with zeros by using the zeros() function. This function creates an array of the specified shape and data type, with all elements initialized to zero.

import numpy as np

# Creating a 3x3 array filled with zeros
arr = np.zeros((3, 3))
print(arr)

In this example, the zeros() function creates a 3×3 array where all elements are set to zero.

Creating arrays filled with zeros is useful in scenarios where you need a placeholder for numerical operations, such as initializing matrices or storing intermediate results in calculations.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top