0
0
NumPydata~10 mins

Slicing with start:stop:step in NumPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Slicing with start:stop:step
Start with array
Apply slice start:stop:step
Calculate indices from start to stop by step
Extract elements at these indices
Return sliced array
We take an array, pick elements starting at 'start', up to but not including 'stop', stepping by 'step', and return those elements.
Execution Sample
NumPy
import numpy as np
arr = np.array([10,20,30,40,50,60])
slice_arr = arr[1:5:2]
print(slice_arr)
This code slices the array from index 1 to 4 stepping by 2, extracting elements at indices 1 and 3.
Execution Table
StepActionIndices CalculatedElements SelectedResulting Slice
1Start with array-[10, 20, 30, 40, 50, 60][10, 20, 30, 40, 50, 60]
2Apply slice 1:5:2Indices 1, 3--
3Extract elements at indices-[20, 40]-
4Return sliced array--[20, 40]
💡 Slicing stops before index 5; step 2 selects indices 1 and 3.
Variable Tracker
VariableStartAfter slice
arr[10, 20, 30, 40, 50, 60][10, 20, 30, 40, 50, 60]
slice_arrN/A[20, 40]
Key Moments - 3 Insights
Why does the slice stop before index 5 and not include it?
In slicing, the stop index is exclusive, so elements are taken up to but not including index 5, as shown in execution_table step 2.
What happens if the step is negative?
A negative step means slicing goes backward. Indices are calculated from start down to stop+1, stepping backward. This changes which elements are selected.
What if start or stop is omitted?
If start is omitted, slicing starts at the beginning (index 0). If stop is omitted, slicing goes to the end of the array. Step defaults to 1 if omitted.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what elements are selected at step 3?
A[30, 50]
B[10, 30]
C[20, 40]
D[40, 60]
💡 Hint
Check the 'Elements Selected' column in execution_table row 3.
At which step does the slicing calculate the indices to use?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at the 'Indices Calculated' column in execution_table.
If the step was changed to 1, how would the resulting slice change?
A[10, 20, 30, 40]
B[20, 30, 40, 50]
C[20, 40]
D[30, 50, 60]
💡 Hint
Step 1 means selecting every element from start to stop-1, check variable_tracker for slice_arr.
Concept Snapshot
Slicing syntax: array[start:stop:step]
- start: index to begin (inclusive)
- stop: index to end (exclusive)
- step: how many indices to jump
Defaults: start=0, stop=array length, step=1
Returns a new array with selected elements.
Full Transcript
We start with a numpy array. When we slice with start:stop:step, numpy calculates indices from start up to but not including stop, jumping by step. Then it extracts elements at those indices and returns them as a new array. For example, arr[1:5:2] picks elements at indices 1 and 3. The stop index is exclusive, so index 5 is not included. If step is negative, slicing goes backward. Omitting start or stop uses defaults: start at 0, stop at array end. This slicing method is a quick way to pick parts of an array.