0
0
NumPydata~10 mins

np.arange() for range arrays in NumPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - np.arange() for range arrays
Start with parameters: start, stop, step
Calculate values from start to stop (exclusive) by step
Create array with these values
Return array
np.arange() creates an array of numbers starting from 'start' up to but not including 'stop', increasing by 'step'.
Execution Sample
NumPy
import numpy as np
arr = np.arange(2, 10, 2)
print(arr)
Creates an array starting at 2, up to 10 (exclusive), stepping by 2.
Execution Table
StepCurrent ValueCondition (value < stop)ActionArray State
122 < 10: TrueAdd 2 to array[2]
244 < 10: TrueAdd 4 to array[2, 4]
366 < 10: TrueAdd 6 to array[2, 4, 6]
488 < 10: TrueAdd 8 to array[2, 4, 6, 8]
51010 < 10: FalseStop adding values[2, 4, 6, 8]
💡 Value reached 10 which is not less than stop (10), so loop stops.
Variable Tracker
VariableStartAfter 1After 2After 3After 4Final
value24681010
array[][2][2, 4][2, 4, 6][2, 4, 6, 8][2, 4, 6, 8]
Key Moments - 3 Insights
Why does np.arange() stop before reaching the stop value?
np.arange() includes values starting from 'start' but stops before 'stop' because the condition is value < stop, not <= stop. See execution_table row 5 where 10 < 10 is False.
What happens if the step is negative?
If step is negative, np.arange() counts down from start to stop (exclusive), adding values while value > stop. The direction depends on step sign.
Can np.arange() include the stop value?
Usually no, because np.arange() excludes stop. To include stop, you can adjust stop or use np.linspace().
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the array state after step 3?
A[2, 4]
B[2, 4, 6]
C[2, 4, 6, 8]
D[2]
💡 Hint
Check the 'Array State' column in execution_table row 3.
At which step does the condition become false and stop adding values?
AStep 5
BStep 3
CStep 4
DStep 2
💡 Hint
Look at the 'Condition' column in execution_table row 5.
If we change step to 3, how would the array change after step 3?
A[2, 6, 9]
B[2, 4, 6]
C[2, 5, 8]
D[2, 3, 6]
💡 Hint
Calculate values starting at 2, adding 3 each time, less than 10.
Concept Snapshot
np.arange(start, stop, step) creates an array from start up to but not including stop, stepping by step.
- Default start is 0, default step is 1.
- Values are added while value < stop (if step > 0).
- For negative step, values added while value > stop.
- Stop value is excluded.
- Useful for creating ranges of numbers quickly.
Full Transcript
np.arange() is a function in numpy that creates arrays of numbers starting from a given start value, increasing by a step size, and stopping before a stop value. It works like a number line segment generator. For example, np.arange(2, 10, 2) creates [2, 4, 6, 8]. The function checks at each step if the current value is less than the stop value (for positive steps) and adds it to the array if true. When the value reaches or passes the stop, it stops adding. This behavior is shown step-by-step in the execution table. Beginners often wonder why the stop value is not included; this is because the condition is strictly less than stop. Changing the step size changes the values generated. np.arange() is a simple and fast way to create numeric ranges for data science tasks.