Bird
Raised Fist0
NumPydata~20 mins

Why indexing matters in NumPy - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Indexing Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of slicing vs. fancy indexing in NumPy
What is the output of the following code snippet?
NumPy
import numpy as np
arr = np.array([10, 20, 30, 40, 50])
slice_view = arr[1:4]
slice_view[0] = 99
print(arr)
fancy_index = arr[[1, 2, 3]]
fancy_index[0] = 77
print(arr)
A[10 20 30 40 50]\n[10 20 30 40 50]
B[10 20 30 40 50]\n[10 77 30 40 50]
C[10 99 30 40 50]\n[10 99 30 40 50]
D[10 99 30 40 50]\n[10 20 30 40 50]
Attempts:
2 left
💡 Hint
Remember that slicing returns a view, while fancy indexing returns a copy.
❓ data_output
intermediate
1:30remaining
Number of elements selected by boolean indexing
Given the array and boolean mask below, how many elements are selected?
NumPy
import numpy as np
arr = np.array([5, 10, 15, 20, 25, 30])
mask = arr > 15
selected = arr[mask]
print(len(selected))
A3
B4
C2
D5
Attempts:
2 left
💡 Hint
Count how many values in arr are greater than 15.
🔧 Debug
advanced
1:30remaining
Why does this indexing code raise an error?
What error does the following code raise and why?
NumPy
import numpy as np
arr = np.array([[1, 2], [3, 4], [5, 6]])
print(arr[[0, 2], 1, 0])
AIndexError: too many indices for array
BTypeError: unhashable type: 'list'
CValueError: shape mismatch
DNo error, prints [2 6]
Attempts:
2 left
💡 Hint
Check how many indices are used for a 2D array.
🚀 Application
advanced
2:00remaining
Selecting rows with multiple conditions using indexing
Which code correctly selects rows from the array where the first column is > 2 and the second column is < 5?
NumPy
import numpy as np
arr = np.array([[1, 4], [3, 2], [5, 6], [4, 3]])
Aarr[arr[:, 0] > 2, arr[:, 1] < 5]
Barr[arr[:, 0] > 2 and arr[:, 1] < 5]
Carr[(arr[:, 0] > 2) & (arr[:, 1] < 5)]
Darr[(arr[:, 0] > 2) | (arr[:, 1] < 5)]
Attempts:
2 left
💡 Hint
Use bitwise operators (&, |) for element-wise logical operations in NumPy.
🧠 Conceptual
expert
2:30remaining
Why does modifying a slice affect the original array but modifying a fancy index does not?
Choose the best explanation for why modifying a slice of a NumPy array changes the original array, but modifying a fancy indexed array does not.
ASlices create copies of the data; fancy indexing returns views that share memory.
BSlices return views that share memory with the original array; fancy indexing returns a copy with separate memory.
CBoth slices and fancy indexing return copies, but slices are linked to the original array by reference.
DFancy indexing modifies the original array only if the array is 1D; slices always modify the original.
Attempts:
2 left
💡 Hint
Think about memory sharing between the original array and the indexed result.

Practice

(1/5)
1. What does indexing in NumPy allow you to do?
easy
A. Create new arrays from scratch
B. Select specific elements or slices from an array
C. Automatically sort the array elements
D. Convert arrays into lists

Solution

  1. Step 1: Understand the purpose of indexing

    Indexing is used to access specific parts of an array, like single elements or slices.
  2. Step 2: Compare with other options

    Creating new arrays, sorting, or converting are different operations, not indexing.
  3. Final Answer:

    Select specific elements or slices from an array -> Option B
  4. Quick Check:

    Indexing = selecting parts of array [OK]
Hint: Indexing means picking parts of data quickly [OK]
Common Mistakes:
  • Confusing indexing with array creation
  • Thinking indexing sorts data
  • Mixing indexing with data type conversion
2. Which of the following is the correct syntax to get the first element of a NumPy array arr?
easy
A. arr.first()
B. arr(0)
C. arr[0]
D. arr{0}

Solution

  1. Step 1: Recall NumPy indexing syntax

    NumPy arrays use square brackets with zero-based indices to access elements.
  2. Step 2: Check each option

    Only arr[0] uses correct square brackets and zero index; others are invalid syntax or methods.
  3. Final Answer:

    arr[0] -> Option C
  4. Quick Check:

    First element = arr[0] [OK]
Hint: Use square brackets and zero-based index [OK]
Common Mistakes:
  • Using parentheses instead of brackets
  • Using curly braces which are invalid
  • Assuming methods like .first() exist
3. What is the output of the following code?
import numpy as np
arr = np.array([10, 20, 30, 40, 50])
print(arr[1:4])
medium
A. [20 30 40]
B. [10 20 30 40]
C. [20 30 40 50]
D. [30 40 50]

Solution

  1. Step 1: Understand slicing syntax

    The slice arr[1:4] selects elements from index 1 up to but not including 4.
  2. Step 2: Identify elements at these indices

    Indices 1, 2, 3 correspond to values 20, 30, 40.
  3. Final Answer:

    [20 30 40] -> Option A
  4. Quick Check:

    Slice 1:4 excludes index 4 [OK]
Hint: Slice excludes end index, so 1:4 means 1,2,3 [OK]
Common Mistakes:
  • Including the end index element
  • Confusing zero-based indexing
  • Printing the whole array by mistake
4. What is wrong with this code snippet?
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr[5])
medium
A. Index 5 is out of bounds for the array
B. The array is not defined properly
C. The print statement syntax is incorrect
D. The array should be a list, not a NumPy array

Solution

  1. Step 1: Check array length and indexing

    The array has 5 elements with indices 0 to 4; index 5 is invalid.
  2. Step 2: Identify error cause

    Accessing arr[5] causes an IndexError because it is out of bounds.
  3. Final Answer:

    Index 5 is out of bounds for the array -> Option A
  4. Quick Check:

    Index must be less than array length [OK]
Hint: Array indices go from 0 to length-1 [OK]
Common Mistakes:
  • Trying to access index equal to array length
  • Confusing array length with max index
  • Assuming Python allows out-of-range indexing
5. Given a 2D NumPy array arr = np.array([[1,2,3],[4,5,6],[7,8,9]]), which indexing expression will select the second column as a 1D array?
hard
A. arr[1, :]
B. arr[:, 2]
C. arr[1]
D. arr[:, 1]

Solution

  1. Step 1: Understand 2D array indexing

    In 2D arrays, arr[:, 1] means all rows, column index 1 (second column).
  2. Step 2: Check other options

    arr[1, :] selects second row, arr[1] is same as second row, arr[:, 2] selects third column.
  3. Final Answer:

    arr[:, 1] -> Option D
  4. Quick Check:

    All rows, column 1 = second column [OK]
Hint: Use colon for all rows, index for column [OK]
Common Mistakes:
  • Mixing row and column indices
  • Using single index for 2D array
  • Selecting wrong column index
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