Bird
Raised Fist0
NumPydata~10 mins

Single element access in NumPy - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to access the element at row 1, column 2 in the numpy array.

NumPy
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
element = arr[1]
Drag options to blanks, or click blank then click option'
A1, 2
B[1, 2]
C[1][2]
D(1, 2)
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses instead of square brackets.
Using one index instead of two.
Using one-based indexing.
2fill in blank
medium

Complete the code to get the element at the first row and last column of the array.

NumPy
import numpy as np
arr = np.array([[7, 8, 9], [10, 11, 12]])
element = arr[1]
Drag options to blanks, or click blank then click option'
A[0, -1]
B[1, -1]
C[0, 2]
D[1, 2]
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 instead of 0 for the first row.
Using positive index for last column without knowing the size.
Using parentheses instead of square brackets.
3fill in blank
hard

Fix the error in the code to correctly access the element at row 2, column 0.

NumPy
import numpy as np
arr = np.array([[1, 2], [3, 4], [5, 6]])
element = arr[1]
Drag options to blanks, or click blank then click option'
A[3, 0]
B(2, 0)
C[2, 0]
D[2][0]
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses instead of square brackets.
Using an index out of range like 3.
Using chained indexing like arr[2][0] which works but is less efficient.
4fill in blank
hard

Fill both blanks to create a dictionary with words as keys and their lengths as values, but only for words longer than 3 letters.

NumPy
words = ['apple', 'bat', 'carrot', 'dog']
lengths = {word: [1] for word in words if [2]
Drag options to blanks, or click blank then click option'
Alen(word)
Blen(word) > 3
Cword > 3
Dword.length > 3
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself in the condition instead of its length.
Using incorrect syntax for length check.
Not using len() function.
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase words as keys and their lengths as values, only for words longer than 4 letters.

NumPy
words = ['tree', 'house', 'car', 'elephant']
result = { [1]: [2] for word in words if [3] }
Drag options to blanks, or click blank then click option'
Aword.upper()
Blen(word)
Clen(word) > 4
Dword.lower()
Attempts:
3 left
💡 Hint
Common Mistakes
Using lower() instead of upper().
Not filtering words by length.
Using word instead of len(word) for length check.

Practice

(1/5)
1. What does single element access in a numpy array allow you to do?
easy
A. Sort the entire array
B. Add new rows to the array
C. Read or change one specific value in the array
D. Calculate the sum of all elements

Solution

  1. Step 1: Understand the concept of single element access

    Single element access means selecting one value from the array using its position.
  2. Step 2: Identify what single element access can do

    It allows reading or changing that one value, not adding rows, sorting, or summing.
  3. Final Answer:

    Read or change one specific value in the array -> Option C
  4. Quick Check:

    Single element access = read/change one value [OK]
Hint: Single element access targets one value by position [OK]
Common Mistakes:
  • Confusing single element access with adding or removing elements
  • Thinking it can perform operations like sum or sort
  • Mixing up element access with slicing
2. Which of the following is the correct syntax to access the element in the 2nd row and 3rd column of a numpy array named arr?
easy
A. arr[2, 3]
B. arr[1, 2]
C. arr(1, 2)
D. arr[2][3]

Solution

  1. Step 1: Recall zero-based indexing in numpy

    Indexing starts at 0, so 2nd row is index 1, 3rd column is index 2.
  2. Step 2: Check syntax for multi-dimensional access

    Use square brackets with comma-separated indexes: arr[1, 2]. Parentheses or double brackets are incorrect here.
  3. Final Answer:

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

    Zero-based indexing + arr[row, col] = arr[1, 2] [OK]
Hint: Use zero-based indexes inside square brackets with commas [OK]
Common Mistakes:
  • Using 1-based indexes instead of zero-based
  • Using parentheses instead of square brackets
  • Using double brackets like arr[2][3] which is less efficient
3. Given the numpy array:
import numpy as np
arr = np.array([[10, 20, 30], [40, 50, 60], [70, 80, 90]])
print(arr[2, 1])

What will be the output?
medium
A. 80
B. 70
C. 60
D. 50

Solution

  1. Step 1: Identify the element at row 2, column 1

    Indexes are zero-based, so row 2 means third row: [70, 80, 90], column 1 means second element.
  2. Step 2: Find the value at arr[2, 1]

    The value is 80.
  3. Final Answer:

    80 -> Option A
  4. Quick Check:

    arr[2,1] = 80 [OK]
Hint: Remember zero-based indexing: row 2 is third row [OK]
Common Mistakes:
  • Confusing row and column indexes
  • Using 1-based indexing
  • Reading the wrong element from the array
4. What is wrong with this code snippet?
import numpy as np
arr = np.array([[1, 2], [3, 4]])
print(arr[2, 1])
medium
A. TypeError because arr is not subscriptable
B. SyntaxError due to wrong brackets
C. It prints 3 correctly
D. IndexError because row index 2 is out of bounds

Solution

  1. Step 1: Check array shape and valid indexes

    arr has shape (2, 2), so valid row indexes are 0 and 1 only.
  2. Step 2: Identify the error from arr[2, 1]

    Row index 2 is out of bounds, causing IndexError.
  3. Final Answer:

    IndexError because row index 2 is out of bounds -> Option D
  4. Quick Check:

    IndexError from invalid row index [OK]
Hint: Check array shape before accessing indexes [OK]
Common Mistakes:
  • Assuming indexes start at 1
  • Confusing syntax errors with index errors
  • Ignoring array dimensions
5. You have a 3D numpy array data with shape (4, 3, 2). How do you access the single element in the 4th block, 2nd row, and 1st column?
hard
A. data[3, 1, 0]
B. data[4, 2, 1]
C. data[3][1][1]
D. data[2, 1, 0]

Solution

  1. Step 1: Understand zero-based indexing for 3D arrays

    4th block means index 3, 2nd row means index 1, 1st column means index 0.
  2. Step 2: Write the correct index tuple

    Use data[3, 1, 0] to access the element.
  3. Final Answer:

    data[3, 1, 0] -> Option A
  4. Quick Check:

    3D array access = data[3,1,0] [OK]
Hint: Count from zero for each dimension in order [OK]
Common Mistakes:
  • Using 1-based indexes instead of zero-based
  • Mixing up the order of indexes
  • Using invalid indexes beyond shape
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