Complete the code to create a 2D array of shape (3, 4) filled with ones.
import numpy as np arr = np.ones([1]) print(arr.shape)
The np.ones function expects a shape tuple, so (3, 4) is correct.
Complete the code to add a 1D array to each row of a 2D array using broadcasting.
import numpy as np A = np.array([[1, 2, 3], [4, 5, 6]]) B = np.array([10, 20, 30]) result = A [1] B print(result)
Using + adds the 1D array B to each row of A by broadcasting.
Fix the error in the code to multiply a (3,1) array with a (3,) array using broadcasting.
import numpy as np A = np.array([[1], [2], [3]]) B = np.array([10, 20, 30]) result = A [1] B print(result)
Multiplying A and B with * uses broadcasting to produce a (3,3) array.
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]
The dictionary comprehension maps each word to its length using len(word), filtering only words with length 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.
[1] = { [2]: [3] for word in words if len(word) > 3 }
The dictionary comprehension maps each uppercase word.upper() to its length len(word) for words longer than 3 characters, stored in result.