Complete the code to create a 2D NumPy array with shape (3, 2).
import numpy as np arr = np.array([[1, 2], [3, 4], [5, 6]]) print(arr[1])
The .shape attribute shows the dimensions of the array. Here, it will print (3, 2).
Complete the code to find the number of dimensions of the array.
import numpy as np arr = np.array([1, 2, 3, 4]) print(arr[1])
The .ndim attribute returns the number of dimensions of the array. Here, it will print 1 because the array is one-dimensional.
Fix the error in the code to reshape the array to 2 rows and 3 columns.
import numpy as np arr = np.arange(6) reshaped = arr[1]((2, 3)) print(reshaped)
The .reshape method reshapes the array to the given shape. It must be called without parentheses after the method name in the code before passing the new shape.
Fill both blanks to create a 3D array with shape (2, 2, 2) and print its number of dimensions.
import numpy as np arr = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) print(arr[1]) print(arr[2])
The first print shows the shape (2, 2, 2). The second print shows the number of dimensions, which is 3.
Complete the code to create a dictionary with word lengths for words longer than 3 characters.
words = ['data', 'science', 'is', 'fun'] lengths = {word: len(word) for word in words if len(word) [1] 3} print(lengths)
The dictionary comprehension uses : to map words to their lengths. The condition uses > to filter words longer than 3 characters. The print statement closes with ).