Complete the code to create a NumPy array from a Python list.
import numpy as np arr = np.[1]([1, 2, 3, 4]) print(arr)
The np.array() function converts a Python list into a NumPy array.
Complete the code to calculate the mean of a NumPy array.
import numpy as np arr = np.array([10, 20, 30, 40]) mean_value = arr.[1]() print(mean_value)
The mean() method calculates the average value of the array elements.
Fix the error in the code to create a 2D NumPy array with shape (2, 3).
import numpy as np arr = np.array([[1, 2, 3], [4, 5, [1]]]) print(arr.shape) # Fix the inner list to have equal length by adding [1]
All inner lists must have the same length to form a proper 2D array. Adding 0 to the second list makes it length 3.
Fill both blanks to create a dictionary comprehension that maps words to their lengths only if length is greater than 3.
words = ['data', 'ai', 'science', 'ml'] lengths = {word: [1] for word in words if [2]
len(words) instead of len(word) causes wrong lengths.word instead of len(word) as the value.The dictionary comprehension maps each word to its length using len(word). The condition len(word) > 3 filters 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 2.
words = ['cat', 'dog', 'a', 'bird'] result = { [1]: [2] for w in words if [3] }
w.lower() instead of w.upper() changes the key format.len(w) > 3 excludes words of length 3.The dictionary comprehension uses w.upper() as keys, len(w) as values, and filters words with length greater than 2.