.fiil()
Anonymous contributor
Published Jul 4, 2024
Contribute to Docs
The .fill()
method is used to fill a NumPy array with a specified scalar value.
Syntax
ndarray.fill(value)
ndarray
: The NumPy array to be filled.value
: The scalar value to assign to all the elements in the array.
Example
In this example, the .fill()
method assigns the scalar value 2798
to all the elements in the one-dimensional NumPy array nf
:
# Import NumPyimport numpy as a# Create a NumPy arraynf = a.arange(12)# Use the '.fill()' methodnf.fill(2798)print(nf)
We will get the following result:
[2798 2798 2798 2798 2798 2798 2798 2798 2798 2798 2798 2798]
Codebyte Example
In the following codebyte example, a two-dimensional NumPy array nf
is created with specific values and then filled entirely with 985
:
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:NumPy on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours