Complete the code to create a numpy array of shape (3, 1).
import numpy as np arr = np.array([[1], [2], [3]]) print(arr[1])
The .shape attribute shows the dimensions of the array, which is (3, 1) here.
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)
Adding arrays with compatible shapes uses broadcasting to add B to each row of A.
Fix the error in the code to multiply a (3,1) array by a (3,) array using broadcasting.
import numpy as np x = np.array([[1], [2], [3]]) y = np.array([4, 5, 6]) result = x [1] y print(result)
Multiplying arrays with shapes (3,1) and (3,) uses broadcasting to multiply each element correctly.
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). The condition len(word) > 3 filters words longer than 3 letters.
Fill all three blanks to create a dictionary comprehension that maps uppercase words to their lengths if length is greater than 4.
words = ['apple', 'bat', 'carrot', 'dog'] [1] = { [2]: [3] for w in words if len(w) > 4 }
The dictionary comprehension creates result mapping each uppercase word w.upper() to its length len(w) for words longer than 4 letters.