Complete the code to filter the array and keep only values greater than 5.
import numpy as np arr = np.array([3, 7, 2, 9, 5]) filtered = arr[arr [1] 5] print(filtered)
== instead of > which selects only values equal to 5.< which selects values less than 5.We use > to select values greater than 5.
Complete the code to filter the array and keep only even numbers.
import numpy as np arr = np.array([1, 4, 7, 8, 10]) even = arr[arr [1] 2 == 0] print(even)
// which does not check for evenness.** which is unrelated.The modulo operator % gives the remainder. Even numbers have remainder 0 when divided by 2.
Fix the error in the code to filter values less than 10.
import numpy as np arr = np.array([12, 5, 8, 15, 3]) filtered = arr[arr [1] 10] print(filtered)
=> which causes syntax error.== which selects only values equal to 10.The operator < selects values less than 10.
Fill both blanks to create a dictionary of words and their lengths for words longer than 3 characters.
words = ['cat', 'elephant', 'dog', 'lion'] lengths = {word: [1] for word in words if [2] print(lengths)
word > 3 which compares string to number and causes error.word instead of length in the dictionary values.We use len(word) to get the length and filter words with length greater than 3.
Fill all three blanks to create a dictionary with uppercase words as keys and their lengths as values, only for words longer than 4 characters.
words = ['apple', 'bat', 'carrot', 'dog'] result = { [1]: [2] for w in words if [3] } print(result)
w.lower() instead of uppercase for keys.len(w) >= 4 which includes words of length 4.Keys are uppercase words using w.upper(), values are lengths with len(w), filtered by length > 4.