0
0
NumPydata~10 mins

Boolean indexing 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 select elements greater than 5 from the array.

NumPy
import numpy as np
arr = np.array([1, 6, 3, 8, 4])
result = arr[arr [1] 5]
print(result)
Drag options to blanks, or click blank then click option'
A<
B==
C>
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '>' selects only elements equal to 5.
Using '<' selects elements less than 5, which is not correct here.
2fill in blank
medium

Complete the code to select elements that are even numbers from the array.

NumPy
import numpy as np
arr = np.array([2, 7, 4, 9, 6])
even_elements = arr[arr [1] 2 == 0]
print(even_elements)
Drag options to blanks, or click blank then click option'
A//
B**
C+
D%
Attempts:
3 left
💡 Hint
Common Mistakes
Using '//' performs floor division, not remainder.
Using '**' is exponentiation, which is not needed here.
3fill in blank
hard

Fix the error in the code to select elements less than 10.

NumPy
import numpy as np
arr = np.array([12, 5, 8, 15, 3])
selected = arr[arr [1] 10]
print(selected)
Drag options to blanks, or click blank then click option'
A<
B==
C!=
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=>' causes syntax error.
Using '==' selects only elements equal to 10, not less than.
4fill in blank
hard

Fill both blanks to create a dictionary of words and their lengths for words longer than 3 characters.

NumPy
words = ['apple', 'bat', 'carrot', 'dog']
lengths = {word: [1] for word in words if [2] > 3}
print(lengths)
Drag options to blanks, or click blank then click option'
Alen(word)
Bword
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'word' instead of 'len(word)' for length calculation.
Filtering by 'word > 3' which is invalid.
5fill in blank
hard

Fill in the blank to create a dictionary of uppercase words and their lengths for words longer than 3 characters.

NumPy
words = ['apple', 'bat', 'carrot', 'dog']
result = [1]
print(result)
Drag options to blanks, or click blank then click option'
A{word.upper(): len(word) for word in words if len(word) > 3}
Blen(word)
C>
D{word.upper()
Attempts:
3 left
💡 Hint
Common Mistakes
Not using uppercase for keys.
Filtering incorrectly or missing length check.