0
0
NumPydata~10 mins

Creating boolean arrays in NumPy - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a boolean array where elements are True if they are greater than 5.

NumPy
import numpy as np
arr = np.array([3, 7, 2, 9, 5])
bool_arr = arr [1] 5
Drag options to blanks, or click blank then click option'
A==
B>
C<
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '>' returns True only for elements equal to 5.
Using '<' or '<=' returns True for smaller or equal elements, not greater.
2fill in blank
medium

Complete the code to create a boolean array where elements are True if they are even numbers.

NumPy
import numpy as np
arr = np.array([1, 4, 7, 8, 10])
bool_arr = (arr [1] 2) == 0
Drag options to blanks, or click blank then click option'
A-
B+
C*
D%
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' or '-' instead of '%' does not check for even numbers.
Using '*' multiplies and does not help in checking evenness.
3fill in blank
hard

Fix the error in the code to create a boolean array where elements are True if less than or equal to 3.

NumPy
import numpy as np
arr = np.array([1, 4, 3, 2, 5])
bool_arr = arr [1] 3
Drag options to blanks, or click blank then click option'
A<
B=>
C<=
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=>' causes syntax errors.
Using '<' excludes elements equal to 3.
Using '==' only matches elements exactly equal to 3.
4fill in blank
hard

Fill both blanks to create a boolean array where elements are True if they are between 3 and 7 inclusive.

NumPy
import numpy as np
arr = np.array([2, 3, 5, 7, 8])
bool_arr = (arr [1] 3) & (arr [2] 7)
Drag options to blanks, or click blank then click option'
A>=
B<
C<=
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' or '<' excludes boundary values.
Using '|' instead of '&' changes logic to 'or' instead of 'and'.
5fill in blank
hard

Fill all three blanks to create a dictionary with words as keys and boolean values indicating if word length is greater than 4.

NumPy
words = ['apple', 'bat', 'carrot', 'dog']
length_check = { [1]: len([2]) [3] 4 for [2] in words }
Drag options to blanks, or click blank then click option'
Aword
C>
Dlen(word)
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'len(word)' as key instead of 'word'.
Using '<' instead of '>' changes the condition.
Using different variable names for key and loop variable causes errors.