Skip to content

NumPy Array Creation

from-list
import numpy as np
 
arr = np.array([1, 2, 3, 4])
print(arr)
print(type(arr))
from-nested
matrix = np.array([
    [1, 2, 3],
    [4, 5, 6]
])
print(matrix)
print(matrix.shape)   # (2, 3)
from-tuple
arr = np.array((10, 20, 30))
print(arr)

Create an array filled with zeros.

zeros
arr = np.zeros((2, 3))
print(arr)
ones
arr = np.ones((3, 2))
print(arr)

Create an array filled with a constant value.

full
arr = np.full((2, 2), 7)
print(arr)
eye
I = np.eye(3)
print(I)

Similar to Python range(), but returns a NumPy array.

arange
arr = np.arange(0, 10, 2)
print(arr)  # [0 2 4 6 8]

Creates num values between start and stop.

linspace
arr = np.linspace(0, 1, 5)
print(arr)  # [0.   0.25 0.5  0.75 1.  ]
dtype
arr = np.array([1, 2, 3], dtype=np.float64)
print(arr)
print(arr.dtype)
diagram Which array-creation function? mermaid
A decision flow for picking the right NumPy constructor based on what data you already have.
  • np.array(...) → convert existing data (lists)
  • np.zeros(shape) / np.ones(shape) → initialize arrays
  • np.full(shape, value) → constant arrays
  • np.eye(n) → identity matrix
  • np.arange(start, stop, step) → integer sequences
  • np.linspace(start, stop, num) → precise evenly spaced floats

Continue to: NumPy Data Types (dtypes) to learn how dtypes affect memory, performance, and numeric precision.

pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading