Complete the code to add a 1D array to each row of a 2D array using broadcasting.
import numpy as np arr2d = np.array([[1, 2, 3], [4, 5, 6]]) arr1d = np.array([10, 20, 30]) result = arr2d [1] arr1d print(result)
Using the plus operator (+) adds the 1D array to each row of the 2D array by broadcasting.
Complete the code to multiply a 2D array by a column vector using broadcasting.
import numpy as np arr2d = np.array([[1, 2, 3], [4, 5, 6]]) col_vec = np.array([[10], [20]]) result = arr2d [1] col_vec print(result)
The multiplication operator (*) broadcasts the column vector across each row of the 2D array.
Fix the error in the code to subtract a 1D array from each column of a 2D array using broadcasting.
import numpy as np arr2d = np.array([[5, 10], [15, 20], [25, 30]]) arr1d = np.array([1, 2, 3]) result = arr2d - arr1d[1] print(result)
Reshaping arr1d to (1,3) aligns it as a row vector to subtract from each column of arr2d by broadcasting.
Fill both blanks to create a dictionary comprehension that maps words to their lengths only if the length is greater than 3.
words = ['apple', 'bat', 'carrot', 'dog'] lengths = {word: [1] for word in words if [2]
The dictionary comprehension maps each word to its length (len(word)) only if the length is greater than 3 (len(word) > 3).
Fill all three blanks to create a dictionary comprehension that maps uppercase words to their lengths only if the length is greater than 2.
words = ['hi', 'hello', 'yes', 'no'] result = { [1]: [2] for word in words if [3] }
The comprehension maps the uppercase version of each word (word.upper()) to its length (len(word)) only if the length is greater than 2 (len(word) > 2).