/Curriculum/BeginnerArray Creation

05. Array Creation FunctionsBeginner

Generate arrays instantly with zeros(), ones(), full(), eye(), identity(), and diag().

15 mins

Concept Overview

NumPy provides specialized C-level factory functions to allocate and initialize arrays with predetermined structures like identity matrices, diagonal vectors, or uniform values.

Hardware Mental Model

Instead of manually building nested loops to fill a matrix, NumPy's creation functions stamp out structured memory buffers in a single CPU instruction.

Key Concepts (1)Click snippet to load in editor

`zeros(shape)` allocates memory initialized to 0. `ones(shape)` fills with 1. `full(shape, val)` fills with a custom scalar.

z = np.zeros((2, 3)); o = np.ones((2, 3)); f = np.full((2, 3), 7)

⚠ Common Mistakes & Pitfalls

Avoid these frequent beginner syntax and logic traps
❌ Incorrect:z = np.zeros(2, 3)
✓ Correct:z = np.zeros((2, 3))

Multi-dimensional shapes must be passed as a tuple `(2, 3)` inside zeros/ones/full.

Pro Tip: Always put double parentheses `np.zeros((rows, cols))` for 2D matrices.

📌 Quick Revision

Core takeaway points from this topic
`np.zeros(shape)`: Creates array filled with zeros.
`np.ones(shape)`: Creates array filled with ones.
`np.full(shape, val)`: Creates array filled with custom value.
`np.eye(N)` & `np.identity(N)`: Creates N x N identity matrix.
`np.diag(v)`: Constructs diagonal matrix from 1D vector.
Editor: 05. Array Creation Functions
Python ● Ready
Initializing Python IDE...
Ctrl+Enter
Execution Result
Click "RUN CODE" to execute and inspect array state

🎯 Try It Yourself: Practice Challenge

Hands-on Mode

Solve the objective below using NumPy vectorized syntax

Objective:Create a 3x3 Identity Matrix

Create a 3x3 identity matrix using `np.eye(3)` and print it.

Expected Output Target:[[1. 0. 0.] [0. 1. 0.] [0. 0. 1.]]
Practice Workspace
Python ● Ready
Initializing Python IDE...
Ctrl+Enter
Execution Result
Click "RUN CODE" to execute and inspect array state