0
0
NumPydata~10 mins

Why boolean masking 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 boolean mask that selects values greater than 5.

NumPy
import numpy as np
arr = np.array([3, 7, 2, 9, 5])
mask = arr [1] 5
print(mask)
Drag options to blanks, or click blank then click option'
A<
B==
C>
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '>' results in a mask selecting only values equal to 5.
Using '<' selects values less than 5, which is the opposite of what we want.
2fill in blank
medium

Complete the code to use the boolean mask to select values greater than 5 from the array.

NumPy
import numpy as np
arr = np.array([3, 7, 2, 9, 5])
mask = arr > 5
selected = arr[1]mask]
print(selected)
Drag options to blanks, or click blank then click option'
A[
B+
C*
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' or '*' tries to add or multiply, which is incorrect for masking.
Forgetting brackets causes a syntax error.
3fill in blank
hard

Fix the error in the code to correctly create a mask for values less than or equal to 4.

NumPy
import numpy as np
arr = np.array([1, 4, 6, 3, 8])
mask = arr [1] 4
print(mask)
Drag options to blanks, or click blank then click option'
A<
B<=
C==
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' excludes values equal to 4.
Using '>=' selects values greater than or equal to 4, which is wrong.
4fill in blank
hard

Complete the code to create a dictionary of words and their lengths, but only for words longer than 3 letters.

NumPy
words = ['cat', 'elephant', 'dog', 'lion']
lengths = {word: len(word) for word in words if len(word) [1] 3}
print(lengths)
Drag options to blanks, or click blank then click option'
A:
B>
C<
D=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' instead of ':' causes syntax errors in dictionary comprehension.
Using '<' filters words shorter than 3, opposite of requirement.
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 with length greater than 3.

NumPy
words = ['apple', 'bat', 'carrot', 'dog']
result = [1]: [2] for w in words if len(w) [3] 3
print(result)
Drag options to blanks, or click blank then click option'
Aw.upper()
Blen(w)
C>
Dw.lower()
Attempts:
3 left
💡 Hint
Common Mistakes
Using w.lower() instead of uppercase keys.
Using '<' instead of '>' filters wrong words.