# numpy.zeros() in Python: Introduction, Syntax & Examples

The numpy.zeros() function in Python’s NumPy library creates an array filled with zeros. This function is particularly useful when we need to initialize an array with zeros before populating it with actual data. It’s commonly used in various numerical and scientific computing tasks. In this article, we will understand Python numpy.zeros() function, its syntax, and learn how to use it with the help of five unique examples. Let’s get started.

Also Read: numpy.cbrt() in Python – Calculating Cube Roots in NumPy

## Syntax of numpy.zeros() Function

Below is the syntax for using numpy.zeros() function in Python.

Syntax:

```numpy.zeros(shape, dtype=float, order='C')
```

Parameters:

• shape: A tuple specifying the dimensions (shape) of the output array. For example, for a 2D array, we would specify the shape as (rows, or columns).
• dtype (optional): The data type of the elements in the array. It’s typically floating, but we can specify other data types as well.
• order (optional): Specifies the memory layout of the array, either ‘C’ (row-major) or ‘F’ (column-major). The default is ‘C’.

Return: An array of zeros with the given shape, data type, and order.

## Examples of numpy.zeros() Function

Let us now look at some examples to demonstrate the use of numpy.zeros() function in Python. In each example, we have passed different parameter combinations to understand how this function works in different scenarios.

### Example 1: Creating a 1D array by specifying the shape

Here we will only provide rows in the numpy.zeros() function.

```import numpy as np
np.zeros(5)
```

In the code above to use the zeros() function we first imported the Numpy library as np then we provided parameter shape as 5 in the np.zeros() function means we provided only row as the dimension of the output array.

Output:

In the output above, we got a 1D array filled with five zeros of data type float as the default data type is float.

### Example 2: Creating a 2D array by specifying the shape

Here we will provide both rows and columns in the numpy.zeros() function.

```np.zeros((2,3))
```

In the code above we provided parameter shape as (2,3) in the np.zeros() function, which means we provided a tuple of integers that helps to create a 2D array of 2 rows and 3 columns.

Output:

In the output above, we got a 2D array filled with zeros of data type float as the default data type is float.

### Example 3: Creating a 1D array by specifying shape & dtype

Here we will provide shape and dtype in the numpy.zeros() function.

```np.zeros(3,dtype="int")
```

In the code above within the np.zeros() function we have provided parameter shape as 3 and we have also provided dtype as int which means we have changed the data type of the element from float to int.

Output:

In the output above, we got a 1D array filled with zeros of data type Integer.

### Example 4: Creating a 2D array by specifying shape & dtype

Here we will do the same thing that we did in the last example but the only difference is we will specify tuple as a parameter for creating a 2D array.

```np.zeros((4,5), dtype="int")
```

In the code above we provided parameter shape as (4,5) in the np.zeros() function to create a 2D array of 4 rows and 5 columns and we also have provided dtype as int.

Output:

In the output above, we got a 2D array filled with zeros of data type Integer.

### Example 5: Creating a 2D array by specifying shape & order

Here in the numpy.zeros() function we will provide shape which is the dimension of the output array and order which specifies the memory layout of the array.

```np.zeros((3,4),order="F")
```

In the code above we provided shape as a tuple which is (3,4) which creates a 2D array filled with zeros of 3 rows and 4 columns and we also provided order as F to store data in column-major order in the memory which is by default C means row-major.

Output:

## Summary

In this tutorial, we have discussed numpy.zeros() function provided by Python’s NumPy library and also explored five examples to create an array filled with zeros using numpy.zeros() function with examples. After reading this tutorial, we hope you can easily create an array filled with zeros in Python.

## Reference

https://stackoverflow.com/questions/69847064/python-for-structure-based-array

Articles: 44