0
0
NumPydata~10 mins

Single element access in NumPy - Interactive Code Practice

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