Complete the code to create a NumPy array.
import numpy as np arr = np.[1]([1, 2, 3, 4])
Use np.array to create a NumPy array from a list.
Complete the code to define a simple custom ufunc using NumPy's vectorize.
import numpy as np @np.vectorize def square(x): return x [1] x
The function squares the input by multiplying it by itself using *.
Fix the error in the custom ufunc that adds 10 to each element.
import numpy as np @np.vectorize def add_ten(x): return x [1] 10
To add 10 to each element, use the addition operator +.
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 len(word) [2] 3}
The dictionary maps each word to its length using len(word), and filters words with length greater than 3 using >.
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'] result = { [1]: [2] for w in words if len(w) [3] 4 }
The dictionary comprehension maps each word in uppercase (w.upper()) to its length (len(w)) only if the length is greater than 4 (>).