Complete the code to create a NumPy array with float32 type.
import numpy as np arr = np.array([1.5, 2.5, 3.5], dtype=[1]) print(arr.dtype)
The dtype argument specifies the data type of the array. Using float32 creates a 32-bit floating point array.
Complete the code to convert an existing array to float64 type.
import numpy as np arr = np.array([1, 2, 3]) arr_float = arr.astype([1]) print(arr_float.dtype)
The astype method converts the array to the specified type. Here, float64 is a 64-bit float type.
Fix the error in the code to create a float16 NumPy array.
import numpy as np arr = np.array([1.1, 2.2, 3.3], dtype=[1]) print(arr.dtype)
To create a float16 array, set dtype to float16. Other types will not produce the desired 16-bit float array.
Fill both blanks to create a dictionary with word lengths only for words longer than 3 characters.
words = ['cat', 'house', 'dog', 'elephant'] lengths = {word: [1] for word in words if len(word) [2] 3} print(lengths)
The dictionary comprehension maps each word to its length using len(word). The condition len(word) > 3 filters words longer than 3 characters.
Fill all three blanks to create a dictionary with uppercase keys and values greater than 0.
data = {'a': 1, 'b': -2, 'c': 3}
result = { [1]: [2] for k, v in data.items() if v [3] 0 }
print(result)The dictionary comprehension uses k.upper() for keys, v for values, and filters values greater than 0 with >.