We use np.zeros() to create arrays filled with zeros. This helps when you need a clean starting point for calculations or data storage.
np.zeros() for zero-filled arrays in NumPy
import numpy as np array = np.zeros(shape, dtype=float, order='C')
shape can be a single number (for 1D array) or a tuple for multi-dimensional arrays.
dtype sets the type of numbers (default is float).
import numpy as np # Create a 1D array of 5 zeros array_1d = np.zeros(5) print(array_1d)
import numpy as np # Create a 2D array (3 rows, 4 columns) of zeros array_2d = np.zeros((3, 4)) print(array_2d)
import numpy as np # Create an empty array with zero elements (edge case) empty_array = np.zeros(0) print(empty_array)
import numpy as np # Create a 1D array of zeros with integer type int_zeros = np.zeros(4, dtype=int) print(int_zeros)
This program creates a 2x3 array filled with zeros, prints it, then changes two values and prints the updated array.
import numpy as np # Create a 2D zero-filled array with 2 rows and 3 columns zero_array = np.zeros((2, 3)) print("Array before filling:") print(zero_array) # Fill the array with some values zero_array[0, 0] = 10 zero_array[1, 2] = 20 print("\nArray after filling some values:") print(zero_array)
Time complexity to create the array is O(n), where n is the total number of elements.
Space complexity is O(n) because it stores all zeros in memory.
Common mistake: forgetting that shape must be a tuple for multi-dimensional arrays, e.g., (3, 4), not just two numbers.
Use np.zeros() when you need a clean array to fill later. For arrays with random values, use np.random functions instead.
np.zeros() creates arrays filled with zeros for easy initialization.
You can create arrays of any shape and data type.
It is useful when you want a blank slate to store or calculate data.
Practice
np.zeros() function do in NumPy?Solution
Step 1: Understand the purpose of
This function is designed to create arrays where every element is zero.np.zeros()Step 2: Compare with other array creation functions
Unlikenp.ones()or random array functions,np.zeros()specifically fills the array with zeros.Final Answer:
Creates an array filled with zeros -> Option AQuick Check:
np.zeros() = zero-filled array [OK]
- Confusing with np.ones()
- Thinking it creates empty arrays
- Assuming it creates random values
Solution
Step 1: Check the shape argument format
The shape must be passed as a tuple, so it should be(3,3).Step 2: Validate the function call syntax
Only parentheses()are valid for function calls, sonp.zeros((3,3))is correct.Final Answer:
np.zeros((3,3)) -> Option CQuick Check:
Shape as tuple inside parentheses [OK]
- Using commas without tuple
- Using square or curly brackets
- Missing parentheses
import numpy as np arr = np.zeros(4, dtype=int) print(arr)
Solution
Step 1: Understand
This creates a 1D array of length 4 filled with zeros, with integer type.np.zeros(4, dtype=int)Step 2: Check the printed output format
Since dtype is int, zeros are printed as integers without decimal points.Final Answer:
[0 0 0 0] -> Option AQuick Check:
Integer zeros printed without decimals [OK]
- Expecting float zeros with decimals
- Confusing with ones array
- Thinking output is boolean
import numpy as np arr = np.zeros[5] print(arr)
Solution
Step 1: Check function call syntax
Functions in Python require parentheses(), not square brackets[].Step 2: Confirm correct usage of np.zeros
The shape argument can be an integer for 1D arrays, so5is valid.Final Answer:
Using square brackets instead of parentheses for function call -> Option BQuick Check:
Function calls need parentheses [OK]
- Using [] instead of ()
- Thinking dtype is mandatory
- Confusing shape tuple for 1D arrays
Solution
Step 1: Check shape argument format
The shape must be a tuple, so(3,4)is correct.Step 2: Verify dtype argument name and value
The correct keyword isdtype, andboolis valid to create boolean arrays.Step 3: Eliminate incorrect options
Using the wrong keywordtypeinstead ofdtype, passing shape as separate arguments like np.zeros(3,4, dtype=bool) without tuple parentheses, using a list[3,4]instead of tuple.Final Answer:
np.zeros((3,4), dtype=bool) -> Option DQuick Check:
Shape tuple and dtype=bool required [OK]
- Passing shape as separate args
- Using list instead of tuple
- Wrong keyword 'type' instead of 'dtype'
Ready to prove it?
Pass the test and this pattern moves unknown
