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] print(result)
Adding a 1D array to a 2D array adds the 1D array to each row 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], [30]]) result = arr2d * [1] print(result)
Multiplying by a column vector broadcasts the vector across columns of the 2D array.
Fix the error in the code to add a 1D array to a 2D array when shapes are incompatible.
import numpy as np arr2d = np.array([[1, 2, 3], [4, 5, 6]]) arr1d = np.array([10, 20]) result = arr2d + [1] print(result)
Reshaping arr1d to (2,1) allows broadcasting to add it to arr2d with shape (2,3).
Fill both blanks to create a dictionary comprehension that maps words to their lengths only if length is greater than 3.
words = ['apple', 'bat', 'carrot', 'dog'] lengths = {word: [1] for word in words if [2] > 3} print(lengths)
The dictionary maps each word to its length if the length is greater than 3.
Fill all three blanks to create a dictionary comprehension that maps uppercase words to their lengths only if length is greater than 3.
words = ['apple', 'bat', 'carrot', 'dog'] lengths = {{ [1]: [2] for word in words if [3] > 3 }} print(lengths)
The dictionary maps each uppercase word to its length if the length is greater than 3.