What is the output of the following code?
import numpy as np arr = np.array([[10, 20, 30], [40, 50, 60]]) result = arr[1, 2] print(result)
Remember that indexing starts at 0 and you access elements by row and column.
The element at row 1, column 2 is 60 because indexing starts at 0. So arr[1, 2] accesses the second row and third column.
Given the 3D array below, what is the value of arr[0, 1, 2]?
import numpy as np arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]]) print(arr[0, 1, 2])
Think of the array as layers, rows, and columns. The first index selects the layer.
arr[0, 1, 2] selects the first layer (index 0), second row (index 1), and third column (index 2), which is 6.
What error will the following code raise?
import numpy as np arr = np.array([1, 2, 3]) result = arr[3] print(result)
Check the size of the array and the index used.
The array has indices 0, 1, 2. Accessing index 3 is out of bounds, so it raises an IndexError.
Given the masked array below, what is the output of masked_arr[1]?
import numpy as np import numpy.ma as ma arr = np.array([10, 20, 30]) masked_arr = ma.masked_array(arr, mask=[False, True, False]) print(masked_arr[1])
Masked elements show as '--' when printed.
The element at index 1 is masked, so printing it shows '--' indicating a masked value.
What is the output of the following code?
import numpy as np arr = np.array([10, 20, 30, 40, 50]) result = arr[[1]][0] print(result)
Remember that arr[[1]] returns a 1-element array, then [0] accesses the first element.
arr[[1]] returns an array with one element [20]. Accessing [0] extracts the scalar 20.