Complete the code to create a NumPy array of zeros with shape (3, 3).
import numpy as np arr = np.[1]((3, 3))
The np.zeros function creates an array filled with zeros, which is useful to avoid temporary arrays when initializing.
Complete the code to multiply two arrays element-wise without creating a temporary array.
import numpy as np arr1 = np.array([1, 2, 3]) arr2 = np.array([4, 5, 6]) result = np.multiply(arr1, arr2, out=[1])
Using the out parameter with arr1 stores the result directly in arr1, avoiding a temporary array.
Fix the error in the code to add 5 to each element of arr without creating a temporary array.
import numpy as np arr = np.array([1, 2, 3]) np.add(arr, [1], out=arr)
Adding a scalar like 5 creates a temporary array. To avoid this, use the out parameter or in-place addition.
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 = { [1]: [2] for word in words if len(word) > 3 }
The dictionary comprehension uses the word as the key and its length as the value, filtering words longer than 3 characters.
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 len(word) [3] 3 }
The comprehension maps uppercase words to their lengths, filtering words with length greater than 3.