NumPy full creates an array with a chosen shape and the same fill value in every cell.

Install NumPy and import it

Install the current NumPy release in your environment, then import it with the short name np used in the examples.

python -m pip install numpy
python -c "import numpy as np; print(np.__version__)"

Create a filled array

Call full with the shape first and the fill value second. A tuple creates a multidimensional array.

import numpy as np

values = np.full((2, 3), 7)
print(values)
print(values.shape)
print(values.dtype)

The shape (2, 3) creates two rows and three columns.

Control the data type

Pass dtype when the fill value should use a specific NumPy type. This is useful when an array will later receive decimal values or when memory layout is part of the design.

import numpy as np

weights = np.full(4, 0.5, dtype=np.float32)
print(weights)
print(weights.dtype)

Fill an array with text or booleans

The fill value can be a string, boolean, or another value that NumPy can represent in the requested array type.

import numpy as np

labels = np.full((2, 2), "pending")
ready = np.full(4, False, dtype=bool)
print(labels)
print(ready)

Choose full instead of zeros or ones

Use full when the initial value carries meaning.

import numpy as np

baseline = np.full(5, 10)
print(baseline)

Avoid shape and type surprises

A scalar shape creates a one-dimensional array, while a tuple describes each axis.

import numpy as np

image = np.full((2, 2, 3), 255, dtype=np.uint8)
print(image.shape)
print(image.dtype)

The shape (2, 2, 3) describes two image rows, two columns, and three channels.

Use np.full when the starting value is part of the data model. The shape, fill value, and dtype make the array predictable.

NumPy full prints a two by three array filled with 7.

Share.
Leave A Reply