0
0
NumPydata~10 mins

Comparison operations 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 boolean array where elements of arr are greater than 5.

NumPy
import numpy as np
arr = np.array([3, 7, 2, 9, 5])
result = 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 '>' which checks for smaller values.
Using '==' which checks for equality, not greater than.
2fill in blank
medium

Complete the code to find elements in arr that are equal to 10.

NumPy
import numpy as np
arr = np.array([10, 15, 10, 20, 25])
mask = arr [1] 10
print(mask)
Drag options to blanks, or click blank then click option'
A>
B!=
C<
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' which checks for inequality.
Using '>' or '<' which check for greater or smaller values.
3fill in blank
hard

Fix the error in the code to correctly create a mask for elements less than or equal to 7.

NumPy
import numpy as np
arr = np.array([5, 8, 7, 10, 3])
mask = arr [1] 7
print(mask)
Drag options to blanks, or click blank then click option'
A<
B<=
C=>
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=>' which is not a valid Python operator.
Using '<' which excludes equality.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps words to their lengths only if the length is greater than 3.

NumPy
words = ['cat', 'elephant', 'dog', 'horse']
lengths = {word: [1] for word in words if [2]
Drag options to blanks, or click blank then click option'
Alen(word)
Blen(word) > 3
Clen(word) < 3
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself as the value instead of its length.
Using the wrong condition like length less than 3.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps uppercase words to their lengths only if length is less than 5.

NumPy
words = ['tree', 'sun', 'flower', 'sky']
result = { [1]: [2] for w in words if [3] }
print(result)
Drag options to blanks, or click blank then click option'
Aw.upper()
Blen(w)
Clen(w) < 5
Dw.lower()
Attempts:
3 left
💡 Hint
Common Mistakes
Using w.lower() instead of w.upper() for keys.
Using wrong condition like length greater than 5.
Using the word itself as value instead of length.