zeros, ones, full & empty
Create arrays of a given shape filled with 0, 1, any value, or uninitialised memory — plus the *_like versions that copy another array's shape and dtype.
Watch the lesson
1 video on CodeWithMunnaX
Before the season starts you often know the shape of your data (say 3 players × 14 matches) but not the values. Instead of typing 42 zeros by hand, NumPy can create a ready-made array of that shape — a "scorecard template" you fill in later. This is called pre-allocation.
Definition
np.zeros,np.ones,np.fullandnp.emptycreate a new array of a given shape, filled respectively with0,1, a chosen fill value, or uninitialised (arbitrary leftover) memory. Their_likeversions (zeros_like,ones_like,full_like,empty_like) take the shape and dtype from an existing array.
- Shape — an int for 1D (
5) or a tuple for more dimensions ((2, 4)). - Fill value — the value every element starts with.
- Uninitialised —
np.emptydoes not write anything into the memory, so it contains whatever bytes were there before. It is slightly faster, but you must overwrite every element. - Pre-allocation — creating the full-size array first and filling it later, instead of growing it with
np.append(slow).
Syntax
python
np.zeros(shape, dtype=float)
np.ones(shape, dtype=float)
np.full(shape, fill_value, dtype=None)
np.empty(shape, dtype=float)
np.zeros_like(a, dtype=None)
np.ones_like(a, dtype=None)
np.full_like(a, fill_value, dtype=None)
np.empty_like(a, dtype=None)| Parameter | Meaning | Default |
|---|---|---|
shape | Size of the array: an int (5) or a tuple ((3, 14)) | required |
fill_value | (full only) value to put in every element | required |
dtype | Data type of the elements | float64 for zeros/ones/empty; for full, inferred from fill_value |
a | (_like only) existing array whose shape (and dtype) is copied | required |
Example — zeros, ones and full
python
import numpy as np
print(np.zeros(5))
print(np.zeros((2, 4)))
print(np.zeros(5, dtype=int))
print(np.ones(4))
print(np.ones((2, 3), dtype=int))
print(np.full(5, 100))
print(np.full((2, 3), 6.5))
print(np.full(3, 4).dtype, np.full(3, 4.0).dtype, np.full(3, True).dtype)
print(np.full((2, 2), "DNB"))text
[0. 0. 0. 0. 0.]
[[0. 0. 0. 0.]
[0. 0. 0. 0.]]
[0 0 0 0 0]
[1. 1. 1. 1.]
[[1 1 1]
[1 1 1]]
[100 100 100 100 100]
[[6.5 6.5 6.5]
[6.5 6.5 6.5]]
int64 float64 bool
[['DNB' 'DNB']
['DNB' 'DNB']]How it works:
zerosandonesgive floats by default — notice the dots (0.,1.). Passdtype=intfor integers.- For 2D, the shape is a tuple:
(2, 4)= 2 rows, 4 columns. np.fullinfers its dtype from the fill value:4→ int64,4.0→ float64,True→ bool,"DNB"(Did Not Bat) → string.
Example — np.empty and pre-allocation
python
import numpy as np
e = np.empty(3)
print(e.shape, e.dtype)
e[:] = [10, 20, 30]
print(e)text
(3,) float64
[10. 20. 30.]We deliberately do not print e before filling it: its contents are arbitrary and change from run to run (sometimes it even looks like zeros, which fools beginners). Only use empty when you are about to overwrite every element.
python
import numpy as np
# Pre-allocate, then fill match by match
season_runs = np.zeros(6, dtype=int)
match_scores = [34, 77, 0, 51, 103, 19]
for i, score in enumerate(match_scores):
season_runs[i] = score
print(season_runs)
print("Total:", season_runs.sum())text
[ 34 77 0 51 103 19]
Total: 284Example — the _like functions
python
import numpy as np
team = np.array([[72, 45, 88],
[56, 0, 74]])
print(np.zeros_like(team))
print(np.ones_like(team))
print(np.full_like(team, 50))
print(np.full_like(team, 7.9))
print(np.zeros_like(team, dtype=float))
print(np.empty_like(team).shape)text
[[0 0 0]
[0 0 0]]
[[1 1 1]
[1 1 1]]
[[50 50 50]
[50 50 50]]
[[7 7 7]
[7 7 7]]
[[0. 0. 0.]
[0. 0. 0.]]
(2, 3)How it works: the result has the same shape and dtype as team (int64). That is why full_like(team, 7.9) gives 7 — the fill value is cast to int. Override with dtype=float when needed.
Comparison
| Function | Fills with | Default dtype | Typical IPL use |
|---|---|---|---|
np.zeros(shape) | 0 | float64 | Empty scorecard / run counter |
np.ones(shape) | 1 | float64 | Default multiplier (pitch factor = 1) |
np.full(shape, v) | v | from v | Target line of 50 for every match |
np.empty(shape) | garbage (uninitialised) | float64 | Fast buffer you fill completely right away |
np.zeros_like(a) etc. | 0 / 1 / v / garbage | same as a | Result array matching an existing table |
Common mistakes
- ❌
np.zeros(2, 3)→TypeError: Cannot interpret '3' as a data type(the 2nd argument is the dtype). ✅np.zeros((2, 3))— pass the shape as one tuple. - ❌ Expecting
np.zeros(5)to give integers. ✅ It gives floats; adddtype=int. - ❌ Reading values from
np.emptybefore writing them. ✅ Usezerosif you are not sure you will fill every element. - ❌
np.full_like(int_array, 7.9)and expecting 7.9. ✅ Adddtype=float.
Interview tip
Q: What is the difference between np.zeros and np.empty?
A: Both allocate an array of the given shape. zeros writes 0 into every element; empty skips that step, so it is marginally faster but contains arbitrary leftover values. Use empty only when every element will be overwritten immediately.
Key points to remember
- np.zeros, np.ones, np.full and np.empty create a new array of a given shape filled with 0, 1, a chosen value, or uninitialised memory.
- Pass the shape as an int for 1D or a tuple for N-D: np.zeros((3, 14)).
- zeros, ones and empty default to float64; np.full infers the dtype from the fill value.
- np.empty is not zero-filled — its values are arbitrary until you overwrite them.
- zeros_like / ones_like / full_like / empty_like copy the shape AND dtype of an existing array.
- Pre-allocating with zeros and filling is far faster than growing an array with np.append.
Try it yourself
Scorecard templates
_like functions copy shape and dtype
Pre-allocate then fill
Practice: bowling slots
Revision Flashcards
Tap a card to flip it and see the answer.
