/Curriculum/BeginnerArray Creation

06. Range & Sequence FunctionsBeginner

Master np.arange(), np.linspace(), and np.logspace() for sequence generation.

12 mins

Concept Overview

Use `np.arange` when you know the step size between points (stop is excluded). Use `np.linspace` when you know the total number of samples desired (stop is included).

Hardware Mental Model

`arange` is walking with a fixed ruler jump (step size). `linspace` is dividing a rope of given length into equal pieces (sample count).

Key Concepts (1)Click snippet to load in editor

`np.arange(start, stop, step)` halts before `stop`. `np.linspace(start, stop, num)` generates exactly `num` evenly spaced points including `stop`.

a = np.arange(0, 10, 2); l = np.linspace(0, 1, 5)

⚠ Common Mistakes & Pitfalls

Avoid these frequent beginner syntax and logic traps
❌ Incorrect:a = np.arange(0, 10, 0.1) # Risk of floating point drift
✓ Correct:a = np.linspace(0, 10, 101)

Using floating-point step in arange can cause inconsistent element counts due to IEEE 754 precision.

Pro Tip: Prefer `linspace` whenever generating fractional or floating-point intervals.

📌 Quick Revision

Core takeaway points from this topic
`np.arange(start, stop, step)`: Step interval based, excludes stop.
`np.linspace(start, stop, num)`: Sample count based, includes stop.
`np.logspace(start, stop, num)`: Logarithmic scale samples.
Editor: 06. Range & Sequence 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:Generate Even Sequence

Generate an array of numbers from 0 up to 10 (exclusive) in steps of 2 using `np.arange` and print it.

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