Challenge - 5 Problems
Slicing Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
Remember slicing syntax is arr[start:stop:step], and stop is exclusive.
✗ Incorrect
The slice starts at index 1 (value 20), stops before index 5, stepping by 2, so it picks indices 1 and 3 (20 and 40).
❓ data_output
intermediate2: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))
Attempts:
2 left
💡 Hint
Count elements starting at index 8 down to but not including 2, stepping by -2.
✗ Incorrect
Indices selected are 8, 6, 4, so length is 3.
🔧 Debug
advanced2: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)
Attempts:
2 left
💡 Hint
Check if step value in slicing can be zero.
✗ Incorrect
Step in slicing cannot be zero; it raises a ValueError.
🚀 Application
advanced2: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]])
Attempts:
2 left
💡 Hint
Remember the first index selects the row, the second index slices the columns.
✗ Incorrect
arr[1, ::3] selects the second row and every third element starting from index 0.
🧠 Conceptual
expert2: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)
Attempts:
2 left
💡 Hint
Omitting start and stop with step -1 reverses the array.
✗ Incorrect
arr[::-1] reverses the array, so output is [25 20 15 10 5].