0
0
NumPydata~10 mins

Combining conditions 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 mask selecting values greater than 5.

NumPy
import numpy as np
arr = np.array([2, 7, 4, 10, 3])
mask = 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 '>' which selects smaller values.
Using '==' which selects only values equal to 5.
2fill in blank
medium

Complete the code to select values greater than 3 and less than 8 using logical AND.

NumPy
import numpy as np
arr = np.array([2, 7, 4, 10, 3])
mask = (arr > 3) [1] (arr < 8)
Drag options to blanks, or click blank then click option'
A^
B|
C&
D~
Attempts:
3 left
💡 Hint
Common Mistakes
Using '|' which means OR instead of AND.
Using '^' which means XOR and gives wrong results.
3fill in blank
hard

Fix the error in combining conditions with parentheses.

NumPy
import numpy as np
arr = np.array([1, 5, 8, 3, 7])
mask = (arr > 3) [1] (arr < 8)
Drag options to blanks, or click blank then click option'
A&
Band
C|
Dor
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'and' which causes a ValueError in NumPy arrays.
Forgetting parentheses around each condition.
4fill in blank
hard

Fill both blanks to create a mask selecting values less than 4 or greater than 7.

NumPy
import numpy as np
arr = np.array([2, 5, 3, 9, 7])
mask = (arr [1] 4) [2] (arr > 7)
Drag options to blanks, or click blank then click option'
A<
B>
C|
D&
Attempts:
3 left
💡 Hint
Common Mistakes
Using '&' instead of '|' which selects values satisfying both conditions.
Using '>' in the first blank which selects wrong values.
5fill in blank
hard

Fill all three blanks to create a dictionary of words and their lengths for words longer than 3 letters and containing 'a'.

NumPy
words = ['apple', 'bat', 'cat', 'dog', 'ant']
result = { [1]: [2] for word in words if len(word) [3] 3 and 'a' in word }
Drag options to blanks, or click blank then click option'
Aword
Blen(word)
C>
Dword.upper()
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'word.upper()' as key changes the word unnecessarily.
Using '<' instead of '>' in the length condition.