0
0
NumPydata~10 mins

Why indexing matters in NumPy - Test Your Understanding

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

Complete the code to create a NumPy array from a list.

NumPy
import numpy as np
arr = np.array([1])
print(arr)
Drag options to blanks, or click blank then click option'
A[1, 2, 3, 4]
B(1, 2, 3, 4)
C1, 2, 3, 4
D{1, 2, 3, 4}
Attempts:
3 left
💡 Hint
Common Mistakes
Using a tuple or set instead of a list inside np.array()
Forgetting the brackets around the numbers
2fill in blank
medium

Complete the code to access the third element of the NumPy array.

NumPy
import numpy as np
arr = np.array([10, 20, 30, 40, 50])
third_element = arr[[1]]
print(third_element)
Drag options to blanks, or click blank then click option'
A1
B0
C3
D2
Attempts:
3 left
💡 Hint
Common Mistakes
Using 3 instead of 2 for the third element
Confusing 1-based indexing with 0-based indexing
3fill in blank
hard

Fix the error in the code to correctly slice the first three elements of the array.

NumPy
import numpy as np
arr = np.array([5, 10, 15, 20, 25])
slice_arr = arr[[1]]
print(slice_arr)
Drag options to blanks, or click blank then click option'
A[0:3]
B0,3
C0:3
D(0:3)
Attempts:
3 left
💡 Hint
Common Mistakes
Using commas instead of colon for slicing
Adding extra brackets around the slice
4fill in blank
hard

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

NumPy
words = ['cat', 'elephant', 'dog', 'horse']
lengths = { [1] : [2] for word in words if len(word) > 3 }
print(lengths)
Drag options to blanks, or click blank then click option'
Aword
Blen(word)
Cword.upper()
Dword.lower()
Attempts:
3 left
💡 Hint
Common Mistakes
Using the length as the key and the word as the value
Using transformations like upper or lower instead of the original word
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 3 characters.

NumPy
words = ['apple', 'bat', 'carrot', 'dog']
lengths = { [1] : [2] for word in words if len(word) [3] 3 }
print(lengths)
Drag options to blanks, or click blank then click option'
Aword.upper()
Blen(word)
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase or original words as keys
Using wrong comparison operators in the condition