Learn / NumPy
Beginner

What Is an Array?

ndarray vs. Python list, and the core ways to create one.

A Python list can hold anything and grow freely, but it's slow for math and doesn't understand "elementwise" operations. NumPy's core type, ndarray (n-dimensional array), trades that flexibility for speed: every element is the same fixed , packed contiguously in memory, so operations run as fast, C loops instead of a Python for loop.

That's the single most important mental shift coming from plain Python: operators act elementwise on arrays. a * 2 doubles every element; a + b adds them pairwise (as long as their agree; more on in Lesson 3).

FunctionWhat it makesExample

np.array(list)

an array from existing data

np.array([1, 2, 3])array([1, 2, 3])

np.zeros(shape) / np.ones(shape)

filled with 0s / 1s

np.zeros(3)array([0., 0., 0.])

np.full(shape, value)

filled with a constant

np.full(3, 7)array([7, 7, 7])

np.arange(start, stop, step)

like range(), but returns an array

np.arange(0, 10, 2)array([0, 2, 4, 6, 8])

np.linspace(start, stop, n)

n evenly spaced points, inclusive of both ends

np.linspace(0, 1, 5)array([0., 0.25, 0.5, 0.75, 1.])

np.eye(n)

the n×n identity matrix

np.eye(3)[[1,0,0],[0,1,0],[0,0,1]]

💡 Tip: shape is always a tuple, even for 1-D arrays: a 1-D array of 5 elements has shape (5,); note the trailing comma, not (5).

Now practice it

Exercises that use what this lesson just covered.

← All NumPy lessonsIndexing & Slicing