Complete the code to create a NumPy array of integers.
import numpy as np arr = np.array([1, 2, 3], dtype="[1]") print(arr.dtype)
The dtype int32 creates an integer array. This is important for type promotion in operations.
Complete the code to add an integer array and a float array, showing type promotion.
import numpy as np arr_int = np.array([1, 2, 3], dtype='int32') arr_float = np.array([1.0, 2.0, 3.0], dtype="[1]") result = arr_int + arr_float print(result.dtype)
Adding int32 and float32 arrays promotes the result to float32 to avoid losing decimal information.
Fix the error in the code to correctly multiply an integer array by a float scalar with type promotion.
import numpy as np arr = np.array([1, 2, 3], dtype='int16') scalar = [1] result = arr * scalar print(result.dtype)
Using a float scalar (2.0) promotes the integer array to float in the multiplication result.
Fill both blanks to create a dictionary comprehension that maps words to their lengths only if length is greater than 3.
words = ['data', 'science', 'ai', 'ml'] lengths = {word: [1] for word in words if [2] > 3} print(lengths)
The comprehension uses len(word) to get length and filters words with length greater than 3.
Fill all three blanks to create a dictionary comprehension that maps uppercase words to their lengths if length is greater than 2.
words = ['cat', 'dog', 'a', 'bird'] [1] = {word.[2](): [3] for word in words if len(word) > 2} print(result)
The dictionary comprehension maps each word in uppercase to its length, filtering words longer than 2 characters.