0
0
NumPydata~10 mins

Boolean type 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 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'
Afloat
Bint
Cbool
Dstr
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'int' or 'float' as dtype instead of 'bool'.
Forgetting to specify dtype and getting default integer array.
2fill in blank
medium

Complete 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'
A!=
B>
C<
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '>' causing wrong mask.
Using '<' which selects smaller values.
3fill in blank
hard

Fix 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'
Abool
Bfloat
Cint
Dstr
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.
4fill in blank
hard

Fill 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'
A%
B==
C!=
D//
Attempts:
3 left
💡 Hint
Common Mistakes
Using '//' instead of '%' for remainder.
Using '!=' instead of '==' causing odd numbers to be True.
5fill in blank
hard

Fill 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'
A.upper()
B>
C!=
D.lower()
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'.