0
0
NumPydata~5 mins

Slicing with start:stop:step in NumPy - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the slicing notation start:stop:step mean in numpy arrays?
It means to select elements from the array starting at index start, up to but not including stop, taking every stepth element.
Click to reveal answer
beginner
What happens if you omit start in start:stop:step slicing?
If start is omitted, slicing starts from the beginning of the array (index 0).
Click to reveal answer
intermediate
How does a negative step value affect slicing in numpy?
A negative step means slicing goes backwards, selecting elements in reverse order.
Click to reveal answer
beginner
Given arr = np.array([10, 20, 30, 40, 50]), what does arr[1:4:2] return?
It returns array([20, 40]) because it starts at index 1 (20), goes up to index 4 (not including 4), taking every 2nd element.
Click to reveal answer
beginner
What is the result of arr[::-1] for a numpy array arr?
It returns the array reversed, selecting all elements with a step of -1.
Click to reveal answer
In numpy slicing arr[start:stop:step], what does stop represent?
AThe last element included in the slice
BThe step size between elements
CThe number of elements to select
DThe index where slicing stops, not including this index
What does arr[::2] do?
ASelects every second element from the start to the end
BSelects elements in reverse order
CSelects elements from index 2 to the end
DSelects elements from index 0 to 2
What is the output of arr[3:0:-1] for arr = np.array([5,10,15,20,25])?
A[20 15 10 5]
B[20 15 10]
C[25 20 15]
D[20 15]
If step is negative, what must be true about start and stop?
A<code>start</code> equals <code>stop</code>
B<code>start</code> must be less than <code>stop</code>
C<code>start</code> must be greater than <code>stop</code>
DNo relation needed
What does arr[:] do?
AReturns a copy of the whole array
BReturns an empty array
CReturns elements from index 1 to end
DReturns elements from index 0 to 1
Explain how slicing with start:stop:step works in numpy arrays.
Think about how you pick items from a list skipping some.
You got /4 concepts.
    Describe what happens when you use a negative step in numpy slicing and give an example.
    Imagine reading a list from the end to the start.
    You got /3 concepts.