Python Central HubSign in

Data Analytics with Python / Phase 2: Numerical Computing NumPy

NumPy Array Creation

Learn how to create NumPy arrays using array(), arange(), linspace(), zeros(), ones(), eye(), full(), and random generators.

Creating arrays from Python objects

From a list

Python
import numpy as np
 
arr = np.array([1, 2, 3, 4])
print(arr)
print(type(arr))

From a nested list (2D)

Python
matrix = np.array([
    [1, 2, 3],
    [4, 5, 6]
])
print(matrix)
print(matrix.shape)   # (2, 3)

From a tuple

Python
arr = np.array((10, 20, 30))
print(arr)

Using built-in constructors

np.zeros()

Create an array filled with zeros.

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

np.ones()

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

np.full()

Create an array filled with a constant value.

Python
arr = np.full((2, 2), 7)
print(arr)

np.eye() (identity matrix)

Python
I = np.eye(3)
print(I)

np.arange() (range with step)

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

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

np.linspace() (even spacing)

Creates num values between start and stop.

Python
arr = np.linspace(0, 1, 5)
print(arr)  # [0.   0.25 0.5  0.75 1.  ]

Specifying dtype during creation

Python
arr = np.array([1, 2, 3], dtype=np.float64)
print(arr)
print(arr.dtype)

Choosing the right constructor

diagram Which array-creation function? mermaid
A decision flow for picking the right NumPy constructor based on what data you already have.

Quick recap: which function to use?

  • 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

Check your understanding

quizCheck yourself
  1. What does np.arange(0, 10, 2) produce?

    Show answer

    A — [0 2 4 6 8] — arange works like range: it starts at 0, steps by 2 and excludes the stop value 10.

  2. What does np.linspace(0, 1, 5) produce?

    Show answer

    C — [0. 0.25 0.5 0.75 1.] — linspace returns the requested number of evenly spaced values including both endpoints, so 5 values from 0 to 1.

  3. What is the shape of np.zeros((2, 3))?

    Show answer

    B — (2, 3) — The tuple passed in is the shape: 2 rows and 3 columns, all zeros.

  4. Which constructor builds a 3 by 3 identity matrix?

    Show answer

    C — np.eye(3) — np.eye(3) puts ones on the diagonal and zeros elsewhere. np.ones and np.full fill every cell with the same value.

Next

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

🧪 Try It Yourself

Exercise 1 – Zeros, Ones, and Full

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 – arange vs linspace

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 – Identity Matrix

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Was this page helpful?

Let us know how we did