Bird
Raised Fist0
NumPydata~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the step value control in a NumPy array slice like arr[start:stop:step]?
easy
A. How many elements to skip between each selected element
B. The total number of elements to select
C. The starting index of the slice
D. The ending index of the slice

Solution

  1. Step 1: Understand the slice syntax

    The slice arr[start:stop:step] selects elements from start up to but not including stop, moving by step.
  2. Step 2: Identify the role of step

    The step determines how many elements to skip between each selected element. For example, step=2 picks every second element.
  3. Final Answer:

    How many elements to skip between each selected element -> Option A
  4. Quick Check:

    Step controls skipping elements = A [OK]
Hint: Step is the gap between elements in slicing [OK]
Common Mistakes:
  • Confusing step with start or stop index
  • Thinking step is total count of elements
  • Ignoring that step can be negative
2. Which of the following is the correct syntax to slice a NumPy array arr to get every 3rd element from index 1 to 10?
easy
A. arr[10:1:3]
B. arr[1:3:10]
C. arr[1:10:3]
D. arr[3:1:10]

Solution

  1. Step 1: Recall slice order

    The slice syntax is arr[start:stop:step]. Here, start=1, stop=10, step=3 means from index 1 up to 9, every 3rd element.
  2. Step 2: Check each option

    arr[1:10:3] matches the correct order and values. Others have wrong order or values that don't match the requirement.
  3. Final Answer:

    arr[1:10:3] -> Option C
  4. Quick Check:

    Correct slice syntax = D [OK]
Hint: Remember slice is start:stop:step in order [OK]
Common Mistakes:
  • Mixing up stop and step positions
  • Using step as stop index
  • Reversing start and stop incorrectly
3. Given arr = np.array([10, 20, 30, 40, 50, 60, 70]), what is the output of arr[1:6:2]?
medium
A. [10 30 50 70]
B. [20 30 40 50 60]
C. [30 50]
D. [20 40 60]

Solution

  1. Step 1: Identify slice indices

    The slice arr[1:6:2] starts at index 1 (value 20), stops before index 6, stepping by 2.
  2. Step 2: Select elements

    Indexes selected: 1 (20), 3 (40), 5 (60). So output is [20 40 60].
  3. Final Answer:

    [20 40 60] -> Option D
  4. Quick Check:

    Elements at 1,3,5 = [20 40 60] [OK]
Hint: Pick elements at start, start+step, ... until stop-1 [OK]
Common Mistakes:
  • Including stop index element
  • Using wrong step size
  • Starting from index 0 instead of 1
4. What is wrong with this code snippet?
arr = np.array([1,2,3,4,5])
slice = arr[4:1:-1]

What will slice contain?
medium
A. [4 3 2]
B. [5 4 3]
C. Empty array
D. IndexError

Solution

  1. Step 1: Understand negative step slicing

    With step=-1, slicing goes backwards from start index 4 down to stop index 1 (exclusive).
  2. Step 2: Identify elements selected

    Indexes selected: 4 (5), 3 (4), 2 (3). So slice contains [5 4 3].
  3. Final Answer:

    [5 4 3] -> Option B
  4. Quick Check:

    Backward slice from 4 to 2 = [5 4 3] [OK]
Hint: Negative step slices backward, stop is exclusive [OK]
Common Mistakes:
  • Expecting forward slice with negative step
  • Including stop index element
  • Thinking it causes error
5. You have a NumPy array arr = np.arange(20). How would you slice it to get every 4th element in reverse order starting from the last element?
hard
A. arr[19::-4]
B. arr[::4]
C. arr[19:0:4]
D. arr[0:19:-4]

Solution

  1. Step 1: Understand array and goal

    arr = np.arange(20) creates array from 0 to 19. We want every 4th element backward starting from last element (index 19).
  2. Step 2: Check slicing options

    arr[19::-4] starts at index 19, goes backward to start (index 0), stepping by 4, which matches the goal.
  3. Step 3: Verify other options

    A: arr[::4] every 4th forward [0,4,8,...], wrong direction. C: arr[19:0:4] positive step with start>stop gives empty. D: arr[0:19:-4] start<stop with negative step gives empty.
  4. Final Answer:

    arr[19::-4] -> Option A
  5. Quick Check:

    Reverse every 4th from last = arr[19::-4] [OK]
Hint: Use start index with negative step for reverse slicing [OK]
Common Mistakes:
  • Using positive step with decreasing indices
  • Wrong start or stop indices
  • Assuming arr[::-4] always starts at last element
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard