Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a boolean array where elements of arr are greater than 5.
NumPy
import numpy as np arr = np.array([3, 7, 2, 9, 5]) result = arr [1] 5 print(result)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' which checks for smaller values.
Using '==' which checks for equality, not greater than.
✗ Incorrect
The '>' operator compares each element of the array to 5, returning True where the element is greater than 5.
2fill in blank
mediumComplete the code to find elements in arr that are equal to 10.
NumPy
import numpy as np arr = np.array([10, 15, 10, 20, 25]) mask = arr [1] 10 print(mask)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' which checks for inequality.
Using '>' or '<' which check for greater or smaller values.
✗ Incorrect
The '==' operator checks element-wise equality with 10, returning True where elements equal 10.
3fill in blank
hardFix the error in the code to correctly create a mask for elements less than or equal to 7.
NumPy
import numpy as np arr = np.array([5, 8, 7, 10, 3]) mask = arr [1] 7 print(mask)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=>' which is not a valid Python operator.
Using '<' which excludes equality.
✗ Incorrect
The correct operator for 'less than or equal to' is '<=', not '=>'.
4fill in blank
hardFill both blanks to create a dictionary comprehension that maps words to their lengths only if the length is greater than 3.
NumPy
words = ['cat', 'elephant', 'dog', 'horse'] lengths = {word: [1] for word in words if [2]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself as the value instead of its length.
Using the wrong condition like length less than 3.
✗ Incorrect
We map each word to its length using len(word), and filter words where length is greater than 3.
5fill in blank
hardFill all three blanks to create a dictionary comprehension that maps uppercase words to their lengths only if length is less than 5.
NumPy
words = ['tree', 'sun', 'flower', 'sky'] result = { [1]: [2] for w in words if [3] } print(result)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using w.lower() instead of w.upper() for keys.
Using wrong condition like length greater than 5.
Using the word itself as value instead of length.
✗ Incorrect
We use w.upper() as the key, len(w) as the value, and filter words with length less than 5.