## Filling NumPy Arrays with Values

Filling NumPy arrays with values is a fundamental operation in scientific computing and data analysis. NumPy, a popular numerical computing library in Python, offers several methods for creating arrays and filling them with specific values.

In this article, we will explore two methods for filling NumPy arrays: using `np.full()`

and `fill()`

.

### Using np.full()

`np.full()`

is a NumPy function that creates an array of a given shape and fills it with a specified value.

The syntax for `np.full()`

is as follows:

`np.full(shape, fill_value, dtype=None, order='C')`

### where:

`shape`

: The shape of the array to be created, specified as a tuple.`fill_value`

: The scalar value to be filled in the array.`dtype`

(optional): The data type of the array. If not specified, the default data type is used.`order`

(optional): The memory layout of the array. If not specified, ‘C’ order (row-major) is used.

Let’s take a look at some examples of using `np.full()`

:

#### Example 1: Creating a 2D array of size (3,4) filled with zeros

```
import numpy as np
a = np.full((3,4), 0)
print(a)
```

### Output:

```
array([[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]])
```

In this example, we create a 2D array of size (3,4) and fill it with zeros using `np.full()`

.

#### Example 2: Creating a 1D array of size 5 filled with the value 3.14

```
import numpy as np
a = np.full(5, 3.14)
print(a)
```

### Output:

`array([3.14, 3.14, 3.14, 3.14, 3.14])`

In this example, we create a 1D array of size 5 and fill it with the value 3.14 using `np.full()`

.

### Using fill()

`fill()`

is a method of the NumPy array class that fills an existing array with a specified value. The syntax for `fill()`

is as follows:

`ndarray.fill(value)`

### where:

`ndarray`

: The NumPy array to be filled.`value`

: The scalar value to be filled in the array.

Let’s take a look at an example of using `fill()`

:

#### Example 3: Filling a 1D array of size 5 with the value 7

```
import numpy as np
a = np.zeros(5)
a.fill(7)
print(a)
```

### Output:

`array([7., 7., 7., 7., 7.])`

In this example, we create a 1D array of size 5 filled with zeros using `np.zeros()`

, and then fill it with the value 7 using `fill()`

.

## Filling an empty NumPy array with a specific value

In addition to `np.full()`

and `fill()`

, NumPy provides the ability to create an empty NumPy array and fill it with a specific value. The syntax for creating an empty NumPy array is as follows:

`np.empty(shape, dtype=float, order='C')`

### where:

`shape`

: The shape of the array to be created, specified as a tuple.`dtype`

(optional): The data type of the array. If not specified, the default data type is float.`order`

(optional): The memory layout of the array. If not specified, ‘C’ order (row-major) is used.

Once we have created an empty NumPy array, we can fill it with a specific value using the `fill()`

method.

#### Example 1: Creating an empty 2D NumPy array of size (3,3) and filling it with the value 1

```
import numpy as np
a = np.empty((3,3))
a.fill(1)
print(a)
```

### Output:

```
array([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
```

In this example, we first create an empty 2D NumPy array of size (3,3) using `np.empty()`

.

We then fill the array with the value 1 using the `fill()`

method.

## Additional resources for learning about NumPy arrays

In addition to the methods discussed in this article, NumPy offers a wide range of functions and methods for creating, manipulating, and analyzing arrays. If you are interested in further learning about NumPy arrays, here are a few resources to get you started:

- NumPy User Guide: The official user guide for NumPy provides a comprehensive introduction to NumPy arrays and their functionality. It covers everything from creating and manipulating arrays to advanced topics like broadcasting and vectorization.
- NumPy Documentation: The NumPy documentation offers a wealth of information on all aspects of the library, including array creation and manipulation, mathematical functions, and linear algebra.
- NumPy Tutorial by W3Schools: This tutorial provides a beginner-friendly introduction to NumPy arrays, with interactive examples to help you get started.
- NumPy Tutorial by DataCamp: This tutorial offers a more in-depth introduction to NumPy arrays, with detailed explanations and interactive exercises to help you master the basics.
- NumPy on Github: The NumPy Github repository offers a wealth of resources for learning about the library, including examples and tutorials, as well as the source code for the library itself.

## Conclusion

In this article, we explored two additional methods for filling NumPy arrays, including filling an empty NumPy array with a specific value and some additional resources for further learning about NumPy arrays. With these methods and resources at your disposal, you will be well on your way to mastering the power and versatility of NumPy arrays for scientific computing and data analysis.

The key takeaway is that NumPy arrays provide a versatile and powerful tool for data manipulation and analysis in Python, and learning how to use them effectively is a vital skill for anyone working in these fields.