Array shape and dtype decide how NumPy calculations line up, broadcast, and store values. Creating the first array from clear input data keeps rows, columns, generated positions, and placeholder grids visible before they feed later code.
Use np.array() when values already exist in Python, np.arange() for integer-spaced positions, and np.zeros() for a fixed empty grid that will be filled later. Passing dtype during creation makes the storage type explicit instead of relying on inference.
The first check should print both the created values and their shape and dtype. Matching the row count, column count, and type before the first calculation catches wrong-input mistakes at the boundary where the array is built.
Related: Convert array dtype
Related: Create linearly spaced values
Related: Reshape an array
Steps to create a NumPy array:
- Create a Python script that builds a typed array, a generated sequence, and a matching zero template.
- array-create.py
import numpy as np measurements = np.array([[12, 14, 15], [10, 13, 16]], dtype=np.int64) sample_points = np.arange(0, 12, 3, dtype=np.int64) template = np.zeros(measurements.shape, dtype=np.float64) row_totals = measurements.sum(axis=1) assert measurements.shape == (2, 3) assert measurements.dtype == np.int64 assert template.shape == measurements.shape print("measurements:") print(measurements) print("shape:", measurements.shape) print("dtype:", measurements.dtype) print("sample points:", sample_points) print("template:") print(template) print("row totals:", row_totals)
Use np.array() for existing nested data, np.arange() for integer steps, and np.zeros() for a numeric layout that will be filled later.
- Run the script and verify the printed array metadata and row totals.
$ python array-create.py measurements: [[12 14 15] [10 13 16]] shape: (2, 3) dtype: int64 sample points: [0 3 6 9] template: [[0. 0. 0.] [0. 0. 0.]] row totals: [41 39]
The assertions stop the script if the main array does not have two rows, three columns, an int64 dtype, or a matching template shape.
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.