Creation Functions Cheat-Sheet
One-page reference of every array-creation function — what it does, its key parameters, default dtype and a real output — plus how to choose the right one.
You now know a dozen ways to create an array. This page collects them in one place so you can quickly pick the right tool — think of it as the team sheet for array creation.
Definition
Array-creation functions are NumPy functions that return a new ndarray, either from existing data (
np.array,np.asarray) or from scratch using a rule — a constant fill (zeros,ones,full,empty), a numeric sequence (arange,linspace,logspace), or a matrix pattern (eye,identity,diag).
The cheat-sheet
| Function | What it creates | Key parameters | Default dtype | Example → output |
|---|---|---|---|---|
np.array(obj) | Array from a list/tuple/nested list | dtype, ndmin | inferred | np.array([45, 78, 12]) → [45 78 12] |
np.zeros(shape) | All 0 | shape, dtype | float64 | np.zeros(3) → [0. 0. 0.] |
np.ones(shape) | All 1 | shape, dtype | float64 | np.ones(3) → [1. 1. 1.] |
np.full(shape, v) | All v | shape, fill_value, dtype | from v | np.full(3, 7) → [7 7 7] |
np.empty(shape) | Uninitialised (garbage) | shape, dtype | float64 | values are arbitrary — fill before use |
np.arange(a, b, s) | Values from a to b (excluded) with step s | start, stop, step | int if all args int, else float | np.arange(1, 6) → [1 2 3 4 5] |
np.linspace(a, b, n) | n evenly spaced values, b included | num, endpoint, retstep | float64 | np.linspace(0, 1, 5) → [0. 0.25 0.5 0.75 1. ] |
np.logspace(a, b, n) | n values from 10^a to 10^b | num, base | float64 | np.logspace(0, 2, 3) → [ 1. 10. 100.] |
np.eye(N, M, k) | 1s on a diagonal, can be rectangular | N, M, k | float64 | np.eye(2) → [[1. 0.] [0. 1.]] |
np.identity(n) | Square identity matrix | n | float64 | np.identity(2) → same as np.eye(2) |
np.diag(v) | Diagonal matrix from 1D v (or extract from 2D) | v, k | from v | np.diag([4, 6]) → [[4 0] [0 6]] |
np.zeros_like(a) | 0s, same shape & dtype as a | a, dtype | same as a | np.zeros_like(np.array([1, 2, 3])) → [0 0 0] |
np.ones_like(a) | 1s, same shape & dtype as a | a, dtype | same as a | — |
np.full_like(a, v) | v, same shape & dtype as a | a, fill_value | same as a | np.full_like(np.array([1.5, 2.5]), 9) → [9. 9.] |
np.empty_like(a) | Uninitialised, same shape & dtype | a | same as a | — |
Note: Random arrays (
np.random.rand,randint,default_rng…) are also creation functions — they get their own chapter.
Example — every function, real output
python
import numpy as np
print(np.array([45, 78, 12]))
print(np.zeros(3))
print(np.ones(3))
print(np.full(3, 7))
print(np.arange(1, 6))
print(np.linspace(0, 1, 5))
print(np.logspace(0, 2, 3))
print(np.eye(2))
print(np.identity(2))
print(np.diag([4, 6]))
print(np.zeros_like(np.array([1, 2, 3])))
print(np.full_like(np.array([1.5, 2.5]), 9))text
[45 78 12]
[0. 0. 0.]
[1. 1. 1.]
[7 7 7]
[1 2 3 4 5]
[0. 0.25 0.5 0.75 1. ]
[ 1. 10. 100.]
[[1. 0.]
[0. 1.]]
[[1. 0.]
[0. 1.]]
[[4 0]
[0 6]]
[0 0 0]
[9. 9.]Which function should I use?
| I want… | Use |
|---|---|
| My own data (scores I typed or read) | np.array(list) |
| A blank scorecard to fill later | np.zeros(shape, dtype=int) |
| Every value the same (target of 50) | np.full(shape, 50) |
| A multiplier array of 1s | np.ones(shape) |
| Integer sequence: overs 1–20 | np.arange(1, 21) |
| Exactly N float values between two numbers | np.linspace(a, b, N) |
| Values spanning powers of 10 | np.logspace / np.geomspace |
| An identity / one-hot matrix | np.eye(n) |
| A matrix with given diagonal values | np.diag(values) |
| A result array matching another array | np.zeros_like(a) / np.full_like(a, v) |
Example — combining creation functions
python
import numpy as np
# Build a season tracker
players = 3
matches = 5
runs = np.zeros((players, matches), dtype=int)
match_no = np.arange(1, matches + 1)
target_line = np.full(matches, 50)
runs[0] = [72, 45, 88, 31, 64]
runs[1] = [56, 0, 74, 88, 32]
runs[2] = [45, 32, 0, 67, 18]
print("Match no:", match_no)
print(runs)
print("Fifties:", (runs >= target_line).sum(axis=1))text
Match no: [1 2 3 4 5]
[[72 45 88 31 64]
[56 0 74 88 32]
[45 32 0 67 18]]
Fifties: [3 3 1]How it works: zeros pre-allocates a 3×5 int table, arange numbers the matches, and full builds a target line of 50s. Assigning a list to runs[0] fills a whole row. runs >= target_line compares every score to 50 (the 1D target is "broadcast" across the rows — covered in the Broadcasting chapter), and .sum(axis=1) counts the True values per player.
Common mistakes (all in one place)
- ❌
np.zeros(2, 3)✅np.zeros((2, 3))— shape is one tuple. - ❌
np.arange(1, 20)for 1–20 ✅np.arange(1, 21)— stop excluded. - ❌
np.linspace(0, 10, 2)meaning "step 2" ✅ 3rd argument is num. - ❌ Reading
np.emptybefore filling ✅ usezeroswhen unsure. - ❌ Expecting ints from zeros/ones/eye/linspace ✅ they default to float64 — pass
dtype=int. - ❌
np.identity(2, 4)✅np.eye(2, 4)for rectangular.
Interview tip
Q: Name three ways to create a 3×3 array of zeros as integers.
A: np.zeros((3, 3), dtype=int), np.full((3, 3), 0), and np.zeros_like(some_3x3_int_array). (Also np.array([[0]*3]*3), but that is the slow, manual way.)
Key points to remember
- Arrays are created either from existing data (np.array) or from scratch with a rule (fills, sequences, matrix patterns).
- Fill functions: zeros, ones, full, empty (+ _like versions that copy another array's shape and dtype).
- Sequence functions: arange (by step, stop excluded), linspace (by count, stop included), logspace (log scale).
- Matrix functions: eye (rectangular/offset), identity (square), diag (build or extract a diagonal).
- zeros, ones, empty, linspace, logspace, eye and identity default to float64.
- Pass shapes as tuples and always give linspace an explicit num.
Try it yourself
Every creation function at a glance
Season tracker built from creation functions
Practice: pick the right function
Revision Flashcards
Tap a card to flip it and see the answer.
