Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
Using '>' compares each element to 5 and returns True if the element is greater than 5.
2fill in blank
mediumComplete 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'
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.
✗ Incorrect
The modulus operator '%' gives the remainder. Even numbers have remainder 0 when divided by 2.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=>' causes syntax errors.
Using '<' excludes elements equal to 3.
Using '==' only matches elements exactly equal to 3.
✗ Incorrect
The correct operator for less than or equal is '<='. '=>' is invalid syntax.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' or '<' excludes boundary values.
Using '|' instead of '&' changes logic to 'or' instead of 'and'.
✗ Incorrect
To check if elements are between 3 and 7 inclusive, use '>=' for lower bound and '<=' for upper bound.
5fill in blank
hardFill 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'
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.
✗ Incorrect
The dictionary comprehension uses 'word' as key, 'len(word) > 4' as condition, and iterates over 'word' in words.