typestar

Creating arrays in Python

The array constructors and shape basics of NumPy.

import numpy as np

zeros = np.zeros(5)
ramp = np.arange(0, 10, 2)
grid = np.linspace(0, 1, 5)
ones = np.ones((2, 3))

data = np.array([[1, 2, 3], [4, 5, 6]])
shape = data.shape
kind = data.dtype
reshaped = data.reshape(3, 2)
flat = data.ravel()

How it works

  1. zeros, arange, and linspace build arrays.
  2. shape and dtype describe an array.
  3. reshape and ravel change its layout.

Keywords and builtins used here

The run, in numbers

Lines
12
Characters to type
245
Tokens
99
Three-star pace
100 tpm

At the three-star pace of 100 tokens a minute, this run takes about 59 seconds.

Type this snippet

Step 1 of 2 in Arrays, step 1 of 22 in NumPy.

Next →