Array creation — np.zeros, np.ones, np.linspace, np.arange, np.random
WHY do we need dedicated creators?
A Python list [0]*1000000 is slow and memory-heavy: each element is a full Python object with a pointer. NumPy instead allocates one typed buffer. So np.zeros(10**6) is fast, compact, and ready for vectorized math. The creators also let you preallocate an output and fill it — the standard fast pattern in scientific code.
np.zeros and np.ones
WHAT they give: a fully-initialized array.
HOW they work: allocate buffer → set every byte (zeros literally memset to 0; ones writes the value 1 in the chosen dtype).
WHY the default dtype is float64: scientific math wants floats; pass dtype=int if you need integers.
Related: np.full(shape, 7) → filled with 7; np.empty(shape) → uninitialized (garbage values, fastest, only safe if you overwrite everything).
np.linspace — N points, endpoints included
Derivation of the spacing (from scratch)
We want num points with , , equally spaced. There are gaps between points. So each gap is
np.arange — fixed step, like range
Derivation of the count
Values are stopping before . The number of elements is
np.random — random arrays
WHY a seed? Reproducible experiments: same seed → same numbers, so a teammate (or your future self) gets identical output.
The mean of rng.random() samples → (uniform on has mean ). The mean of rng.normal(loc, scale, ...) → loc, std → scale, for large samples.

Recall Feynman: explain to a 12-year-old
Imagine an empty egg carton. np.zeros is a carton where every cup holds a "0" egg; np.ones holds "1" eggs. np.linspace is like saying "I want exactly 5 marks painted evenly on a ruler from 0 to 1, including the very ends." np.arange is "start walking from 0 and take 0.25-size steps, stop before you reach 1." np.random is rolling dice to fill the cups with surprise numbers — and a seed is a magic word that makes the dice always land the same way so you can replay the game.
Common mistakes (steel-manned)
Flashcards
What does np.zeros((2,3)) return?
Default dtype of np.zeros / np.ones?
float64.Spacing formula for np.linspace(a,b,num) with endpoint=True?
Does np.linspace include the stop by default?
Does np.arange include the stop value?
range).Number of elements in np.arange(a,b,s)?
Why avoid float steps in np.arange?
linspace.How many points and what step does np.linspace(0,1,5) give?
What does np.empty give that np.zeros doesn't?
Modern way to make a random generator?
rng = np.random.default_rng(seed).Why pass a seed to the RNG?
rng.random((2,2)) range?
Expected mean of many rng.random() samples?
How to make a 4×4 array all equal to 7?
np.full((4,4), 7).Connections
- NumPy arrays — shape, dtype, ndim
- Vectorization vs Python loops
- Array indexing and slicing
- Reshaping — reshape, ravel, newaxis
- Plotting functions with Matplotlib (linspace feeds the x-axis)
- Random sampling and distributions
- Floating-point representation and rounding errors
Concept Map
Hinglish (regional understanding)
Intuition Hinglish mein samjho
Dekho, scientific computing me sabse pehla kaam hota hai numbers ka ek container banana — yahi NumPy array hai: ek hi dtype ka contiguous memory block jiska ek shape hota hai. np.zeros aur np.ones ka kaam simple hai: ek khaali slate de do, sab 0 ya sab 1 se bhara hua. Yaad rakho shape tuple me jaata hai, jaise np.zeros((3,4)) — agar comma se do number diye np.zeros(3,4) to error, kyunki doosra slot dtype ka hai.
np.linspace aur np.arange ka fark exam-favorite hai. linspace(a, b, num) bolta hai "mujhe interval me exactly num points chahiye, dono ends include karke" — step khud calculate hota hai: , kyunki num points ke beech num-1 gaps hote hain (fencepost logic). arange(a, b, s) Python ke range jaisa hai: fixed step s se chalo, par b ko chhod do (exclusive). Plotting ke liye hamesha linspace use karo, aur float steps wale arange se bacho kyunki 0.1 exact store nahi hota, isliye last value aur count kabhi-kabhi galat aa jaata hai.
np.random me modern tareeka hai rng = np.random.default_rng(seed). rng.random() deta hai uniform, rng.normal(mean, std, size) deta hai Gaussian. Seed ka magic ye hai ki same seed → same numbers, to tumhara experiment reproducible ban jaata hai — teammate ko bilkul wahi output milega. Bottom line: blank chahiye to zeros/ones, kitne points chahiye to linspace, kitna bada step chahiye to arange, aur randomness chahiye to random. Itna clear ho gaya to ye topic 80/20 me tumhare paas pakka hai.