📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials NumPy Creating Arrays

Creating Arrays

4 min read Quiz at the end
Create arrays from scratch with zeros, ones, arange, linspace, random, and eye functions.

Creating NumPy Arrays

import numpy as np

np.zeros((3, 4))          # 3x4 array of zeros
np.ones((2, 3))           # 2x3 array of ones
np.full((2, 2), 7)        # 2x2 filled with 7
np.eye(3)                 # 3x3 identity matrix
np.empty((2, 3))          # uninitialized (fast)

np.arange(0, 10, 2)      # [0, 2, 4, 6, 8]
np.linspace(0, 1, 5)     # [0. 0.25 0.5 0.75 1.]
np.logspace(0, 2, 3)     # [1. 10. 100.]

np.random.rand(3, 3)      # uniform [0, 1)
np.random.randn(3, 3)     # standard normal
np.random.randint(0, 10, (3, 3))  # random ints
np.random.seed(42)        # reproducibility
Topic Quiz · 1 questions

Test your understanding before moving on

1. Which function creates an array of evenly-spaced values between 0 and 1?
💡 np.linspace(start, stop, num) returns num evenly-spaced values including both endpoints.