Creating arrays from Python objects
From a list
import numpy as np
arr = np.array([1, 2, 3, 4])
print(arr)
print(type(arr))From a nested list (2D)
matrix = np.array([
[1, 2, 3],
[4, 5, 6]
])
print(matrix)
print(matrix.shape) # (2, 3)From a tuple
arr = np.array((10, 20, 30))
print(arr)Using built-in constructors
np.zeros()
Create an array filled with zeros.
arr = np.zeros((2, 3))
print(arr)np.ones()
arr = np.ones((3, 2))
print(arr)np.full()
Create an array filled with a constant value.
arr = np.full((2, 2), 7)
print(arr)np.eye() (identity matrix)
I = np.eye(3)
print(I)np.arange() (range with step)
Similar to Python range(), but returns a NumPy array.
arr = np.arange(0, 10, 2)
print(arr) # [0 2 4 6 8]np.linspace() (even spacing)
Creates num values between start and stop.
arr = np.linspace(0, 1, 5)
print(arr) # [0. 0.25 0.5 0.75 1. ]Specifying dtype during creation
arr = np.array([1, 2, 3], dtype=np.float64)
print(arr)
print(arr.dtype)Choosing the right constructor
flowchart TD
A["Need an array"] --> B{"Already have data
(list/tuple)?"}
B -- "Yes" --> C["np.array(data)"]
B -- "No" --> D{"What kind of
placeholder?"}
D -- "All zeros" --> E["np.zeros(shape)"]
D -- "All ones" --> F["np.ones(shape)"]
D -- "One constant value" --> G["np.full(shape, value)"]
D -- "Identity matrix" --> H["np.eye(n)"]
D -- "Integer sequence" --> I["np.arange(start, stop, step)"]
D -- "Evenly spaced floats" --> J["np.linspace(start, stop, num)"]
Quick recap: which function to use?
np.array(...)→ convert existing data (lists)np.zeros(shape)/np.ones(shape)→ initialize arraysnp.full(shape, value)→ constant arraysnp.eye(n)→ identity matrixnp.arange(start, stop, step)→ integer sequencesnp.linspace(start, stop, num)→ precise evenly spaced floats
Check your understanding
What does np.arange(0, 10, 2) produce?
arange works like range: it starts at 0, steps by 2 and excludes the stop value 10.
Show answer
A — [0 2 4 6 8] — arange works like range: it starts at 0, steps by 2 and excludes the stop value 10.
What does np.linspace(0, 1, 5) produce?
linspace returns the requested number of evenly spaced values including both endpoints, so 5 values from 0 to 1.
Show answer
C — [0. 0.25 0.5 0.75 1.] — linspace returns the requested number of evenly spaced values including both endpoints, so 5 values from 0 to 1.
What is the shape of np.zeros((2, 3))?
The tuple passed in is the shape: 2 rows and 3 columns, all zeros.
Show answer
B — (2, 3) — The tuple passed in is the shape: 2 rows and 3 columns, all zeros.
Which constructor builds a 3 by 3 identity matrix?
np.eye(3) puts ones on the diagonal and zeros elsewhere. np.ones and np.full fill every cell with the same value.
Show answer
C — np.eye(3) — np.eye(3) puts ones on the diagonal and zeros elsewhere. np.ones and np.full fill every cell with the same value.
Next
Continue to: NumPy Data Types (dtypes) to learn how dtypes affect memory, performance, and numeric precision.
🧪 Try It Yourself
Exercise 1 – Zeros, Ones, and Full
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 2 – arange vs linspace
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 3 – Identity Matrix
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.