Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

arrays
python
communitycreator

What is the numpy.zeros() function in Python?

Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code

Overview

The numpy.zeros() function is used to return a new array of a given shape and type filled with zeros.

Syntax

The function has the following syntax:

numpy.zeros(shape, dtype=float, order='C', *, like=None)

Parameters

The numpy.zeros() function takes the following parameter values:

  • shape: This represents the desired or the new array.
  • dtype: This represents the desired data type of array and is optional.
  • like: This represents the array_like object or the prototype.

Return value

The numpy.zeros() function returns an array with the given shape and data type filled with zeros.

Example

import numpy as np
# creating a prototype or array_like object with an integer data type
my_array = np.zeros(5, dtype = int)
print(my_array)
Implementing the numpy.zeros() function

Explanation

  • Line 1: We import the numpy module.
  • Line 4: We implement the numpy.zeros() function to create an array with its 5 elements set to 0. We assigned the result to a variable my_array.
  • Line 6: We print the new array my_array.

RELATED TAGS

arrays
python
communitycreator

CONTRIBUTOR

Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code
Keep Exploring