0
0
NumPydata~20 mins

Slicing with start:stop:step in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Slicing Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of slicing a NumPy array with positive step
What is the output of the following code snippet?
NumPy
import numpy as np
arr = np.array([10, 20, 30, 40, 50, 60])
sliced = arr[1:5:2]
print(sliced)
A[20 40]
B[20 30 40 50]
C[30 50]
D[10 30 50]
Attempts:
2 left
💡 Hint
Remember slicing syntax is arr[start:stop:step], and stop is exclusive.
data_output
intermediate
2:00remaining
Resulting array length after slicing with negative step
Given the array and slicing below, how many elements does the resulting array have?
NumPy
import numpy as np
arr = np.arange(10)
sliced = arr[8:2:-2]
print(len(sliced))
A2
B4
C3
D5
Attempts:
2 left
💡 Hint
Count elements starting at index 8 down to but not including 2, stepping by -2.
🔧 Debug
advanced
2:00remaining
Identify the error in this slicing code
What error does this code raise?
NumPy
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
sliced = arr[::0]
print(sliced)
ANo error, prints the whole array
BIndexError: index out of range
CTypeError: slice indices must be integers or None
DValueError: slice step cannot be zero
Attempts:
2 left
💡 Hint
Check if step value in slicing can be zero.
🚀 Application
advanced
2:00remaining
Extract every third element from a 2D NumPy array's second row
Given a 2D array, which slicing extracts every third element from the second row?
NumPy
import numpy as np
arr = np.array([[1,2,3,4,5,6,7,8,9],[10,11,12,13,14,15,16,17,18]])
Aarr[::3, 1]
Barr[1, ::3]
Carr[1, 3::]
Darr[1, 3:3]
Attempts:
2 left
💡 Hint
Remember the first index selects the row, the second index slices the columns.
🧠 Conceptual
expert
2:00remaining
Understanding slicing with omitted start and stop and negative step
What is the output of this code?
NumPy
import numpy as np
arr = np.array([5, 10, 15, 20, 25])
sliced = arr[::-1]
print(sliced)
A[25 20 15 10 5]
B[5 10 15 20 25]
C[5 15 25]
D[]
Attempts:
2 left
💡 Hint
Omitting start and stop with step -1 reverses the array.