Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a NumPy boolean array from a list.
NumPy
import numpy as np arr = np.array([True, False, True], dtype=[1]) print(arr)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'int' or 'float' as dtype instead of 'bool'.
Forgetting to specify dtype and getting default integer array.
✗ Incorrect
The dtype 'bool' creates a boolean array in NumPy.
2fill in blank
mediumComplete the code to create a boolean mask where values are greater than 5.
NumPy
import numpy as np arr = np.array([3, 7, 2, 9]) mask = arr [1] 5 print(mask)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '>' causing wrong mask.
Using '<' which selects smaller values.
✗ Incorrect
The '>' operator creates a boolean array where elements greater than 5 are True.
3fill in blank
hardFix the error in the code to convert an integer array to boolean type.
NumPy
import numpy as np arr = np.array([0, 1, 2, 0]) bool_arr = arr.astype([1]) print(bool_arr)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'int' or 'float' in astype which does not convert to boolean.
Using 'str' which converts numbers to strings.
✗ Incorrect
Using astype('bool') converts integers to boolean values where 0 is False and others True.
4fill in blank
hardFill both blanks to create a boolean array where elements are True if even and False if odd.
NumPy
import numpy as np arr = np.array([1, 2, 3, 4, 5]) even_mask = (arr [1] 2) [2] 0 print(even_mask)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '//' instead of '%' for remainder.
Using '!=' instead of '==' causing odd numbers to be True.
✗ Incorrect
The modulo operator '%' finds remainder; comparing with '==' 0 selects even numbers.
5fill in blank
hardFill all three blanks to create a dictionary with words as keys and boolean values indicating if length is greater than 3.
NumPy
words = ['cat', 'lion', 'dog', 'tiger'] length_check = {word[1]: len(word) [2] 3 for word in words if word [3] 'dog'} print(length_check)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '.lower()' instead of '.upper()' for keys.
Using '<' instead of '>' for length comparison.
Using '==' instead of '!=' to exclude 'dog'.
✗ Incorrect
We use word.upper() as key, check length > 3, and exclude 'dog' with '!='.