Question 1 of 60 · Arrays vs lists · Easy
Which Python library provides the ndarray data type?
A
Requests
B
Pandas
C
NumPy
D
Matplotlib
Not quite. Try another option.
Answer: NumPy
NumPy provides the multidimensional array type called ndarray.
Question 2 of 60 · Arrays vs lists · Easy
What does multiplying np.array([1, 2, 3]) by 2 produce?
A
[3, 4, 5]
B
[1, 2, 3, 1, 2, 3]
C
[2, 4, 6]
D
[1, 4, 9]
Not quite. Try another option.
Answer: [2, 4, 6]
Multiplication by a scalar is applied element by element to a NumPy array.
Question 3 of 60 · Arrays vs lists · Easy
What does multiplying the Python list [1, 2] by 2 produce?
A
[2, 2]
B
[1, 4]
C
[1, 2, 1, 2]
D
[2, 4]
Not quite. Try another option.
Answer: [1, 2, 1, 2]
Multiplying a Python list by an integer repeats the list.
Question 4 of 60 · Arrays vs lists · Easy
Which statement about NumPy arrays is generally true?
A
Their indexes always begin at one
B
Their elements share one data type
C
Their size can never be inspected
D
Their elements must all be strings
Not quite. Try another option.
Answer: Their elements share one data type
A NumPy array generally stores its elements using a single common data type.
Question 5 of 60 · Arrays vs lists · Easy
Which attribute gives the dimensions of a NumPy array?
A
keys
B
append
C
shape
D
split
Not quite. Try another option.
Answer: shape
The shape attribute returns a tuple describing the size of each array dimension.
Question 6 of 60 · Array creation routines · Easy
Which function creates a NumPy array filled with zeros?
A
np.full()
B
np.zeros()
C
np.empty()
D
np.ones()
Not quite. Try another option.
Answer: np.zeros()
np.zeros() creates an array whose elements are initialized to zero.
Question 7 of 60 · Array creation routines · Easy
What does np.ones(4) create?
A
An array of four zeros
B
An array of four ones
C
An array containing only four
D
An array from zero to four
Not quite. Try another option.
Answer: An array of four ones
np.ones(4) creates a one-dimensional array containing four values equal to one.
Question 8 of 60 · Array creation routines · Easy
Which function creates evenly spaced values within a specified interval using a requested number of values?
A
np.linspace()
B
np.reshape()
C
np.asarray()
D
np.zeros()
Not quite. Try another option.
Answer: np.linspace()
np.linspace() creates a chosen number of evenly spaced values between two endpoints.
Question 9 of 60 · Array creation routines · Easy
What values are produced by np.arange(1, 5)?
A
[0, 1, 2, 3, 4]
B
[1, 2, 3, 4, 5]
C
[0, 1, 2, 3, 4, 5]
D
[1, 2, 3, 4]
Not quite. Try another option.
Answer: [1, 2, 3, 4]
np.arange() includes the start value but excludes the stop value.
Question 10 of 60 · Array creation routines · Easy
Which expression creates a array filled with zeros?
A
np.ones((2, 3))
B
np.arange((2, 3))
C
np.zeros(2, 3)
D
np.zeros((2, 3))
Not quite. Try another option.
Answer: np.zeros((2, 3))
Passing the shape tuple (2, 3) to np.zeros() creates two rows and three columns.
Question 11 of 60 · Arrays from existing data · Easy
Which expression converts the list [2, 4, 6] into a NumPy array?
A
np.array([2, 4, 6])
B
np.arange([2, 4, 6])
C
np.zeros([2, 4, 6])
D
np.shape([2, 4, 6])
Not quite. Try another option.
Answer: np.array([2, 4, 6])
np.array() can create a NumPy array from an existing Python list.
Question 12 of 60 · Arrays from existing data · Easy
Which existing Python object can be passed directly to np.array()?
A
A module
B
A tuple
C
An import statement
D
A function definition
Not quite. Try another option.
Answer: A tuple
A tuple is a sequence of values and can be converted directly into a NumPy array.
Question 13 of 60 · Arrays from existing data · Easy
What is the shape of np.array([[1, 2], [3, 4]])?
A
(2, 2)
B
(1, 4)
C
(4, 2)
D
(2, 1)
Not quite. Try another option.
Answer: (2, 2)
The nested list has two rows, and each row contains two elements.
Question 14 of 60 · Arrays from existing data · Easy
Which NumPy function can convert existing data to an array while avoiding a copy when possible?
A
np.sort()
B
np.asarray()
C
np.zeros()
D
np.linspace()
Not quite. Try another option.
Answer: np.asarray()
np.asarray() converts compatible input to an array and may reuse existing array data.
Question 15 of 60 · Arrays from existing data · Easy
What is the number of dimensions of np.array([5, 10, 15])?
A
One dimension
B
Two dimensions
C
Three dimensions
D
Zero dimensions
Not quite. Try another option.
Answer: One dimension
A single flat sequence creates a one-dimensional NumPy array.
Question 16 of 60 · Indexing and slicing · Easy
Given a = np.array([10, 20, 30]), what is the value of a[0]?
A
0
B
10
C
30
D
20
Not quite. Try another option.
Answer: 10
NumPy array indexing starts at zero, so index 0 selects the first element.
Question 17 of 60 · Indexing and slicing · Easy
Given a = np.array([10, 20, 30, 40]), what does a[1:3] return?
A
[10, 20]
B
[20, 30]
C
[10, 20, 30]
D
[20, 30, 40]
Not quite. Try another option.
Answer: [20, 30]
The slice starts at index 1 and stops before index 3.
Question 18 of 60 · Indexing and slicing · Easy
Which index selects the last element of a one-dimensional NumPy array?
A
1
B
-1
C
0
D
-2
Not quite. Try another option.
Answer: -1
Negative index -1 refers to the final element of an array.
Question 19 of 60 · Indexing and slicing · Easy
Given a = np.array([[1, 2], [3, 4]]), what is the value of a[1, 0]?
A
4
B
1
C
2
D
3
Not quite. Try another option.
Answer: 3
Index [1, 0] selects the second row and the first column.
Question 20 of 60 · Indexing and slicing · Easy
Given a = np.array([2, 4, 6, 8]), what does a[::2] return?
A
[2, 4]
B
[2, 6]
C
[4, 8]
D
[6, 8]
Not quite. Try another option.
Answer: [2, 6]
A step of 2 selects every second element, beginning with index 0.
Question 21 of 60 · Arrays vs lists · Medium
What is the output of (a + a).tolist(), b + b when a = np.array([1, 2]) and b = [1, 2]?
A
([2, 4], [2, 4])
B
([2, 4], [1, 2, 1, 2])
C
([1, 2, 1, 2], [1, 2, 1, 2])
D
([1, 2, 1, 2], [2, 4])
Not quite. Try another option.
Answer: ([2, 4], [1, 2, 1, 2])
NumPy array addition is element-wise, while adding Python lists concatenates them.
Question 22 of 60 · Arrays vs lists · Medium
Given a = np.array([2, 3]) and b = [2, 3], what are the results of a * 2 and b * 2?
A
array([4, 6]) and [4, 6]
B
array([4, 6]) and [2, 3, 2, 3]
C
array([2, 3, 2, 3]) and [4, 6]
D
array([2, 3]) and [2, 3]
Not quite. Try another option.
Answer: array([4, 6]) and [2, 3, 2, 3]
Multiplication scales each array element, but it repeats a Python list.
Question 23 of 60 · Arrays vs lists · Medium
What does np.array([1, 2]) + [10, 20] produce?
A
[11, 22]
B
A TypeError
C
array([1, 2, 10, 20])
D
array([11, 22])
Not quite. Try another option.
Answer: array([11, 22])
NumPy converts the list to a compatible array and performs element-wise addition.
Question 24 of 60 · Arrays vs lists · Medium
What is the result of np.array([1, 2.5, 3]).dtype on a typical NumPy installation?
A
An object dtype
B
A floating-point dtype
C
An integer dtype
D
A Boolean dtype
Not quite. Try another option.
Answer: A floating-point dtype
NumPy selects one common dtype, so the integers are promoted to floating-point values.
Question 25 of 60 · Arrays vs lists · Medium
Which statement correctly compares a NumPy array with a Python list?
A
A list supports vectorized arithmetic, while an array does not
B
An array usually stores one dtype, while a list may mix types
C
A list always uses less memory than an equivalent array
D
An array may mix arbitrary types without changing its dtype
Not quite. Try another option.
Answer: An array usually stores one dtype, while a list may mix types
NumPy arrays normally use a single dtype, whereas lists can hold references to objects of different types.
Question 26 of 60 · Array creation routines · Medium
What array is created by np.arange(2, 11, 3)?
A
array([2, 5, 8])
B
array([3, 6, 9])
C
array([2, 5, 8, 11])
D
array([2, 4, 6, 8, 10])
Not quite. Try another option.
Answer: array([2, 5, 8])
arange starts at 2, advances by 3, and excludes the stop value 11.
Question 27 of 60 · Array creation routines · Medium
Which result is produced by np.linspace(0, 1, 5)?
A
array([0.0, 0.2, 0.4, 0.6, 0.8])
B
array([0.0, 0.5, 1.0, 1.5, 2.0])
C
array([0.0, 0.25, 0.5, 0.75])
D
array([0.0, 0.25, 0.5, 0.75, 1.0])
Not quite. Try another option.
Answer: array([0.0, 0.25, 0.5, 0.75, 1.0])
linspace creates five evenly spaced values and includes both endpoints by default.
Question 28 of 60 · Array creation routines · Medium
After a = np.zeros((2, 3)), what are a.shape and a.size?
A
(6,) and 6
B
(2, 3) and 6
C
(3, 2) and 6
D
(2, 3) and 5
Not quite. Try another option.
Answer: (2, 3) and 6
The array has two rows and three columns, giving elements.
Question 29 of 60 · Array creation routines · Medium
Which expression creates a integer array in which every element is 7?
A
np.zeros((2, 3), dtype=int) + 1
B
np.arange(7, 13).reshape(2, 3)
C
np.ones((2, 3), dtype=int)
D
np.full((2, 3), 7, dtype=int)
Not quite. Try another option.
Answer: np.full((2, 3), 7, dtype=int)
np.full creates an array of the requested shape filled with the specified value.
Question 30 of 60 · Array creation routines · Medium
How many elements equal 1 in the array created by np.eye(3, k=1)?
A
1
B
2
C
3
D
6
Not quite. Try another option.
Answer: 2
k=1 places ones on the diagonal immediately above the main diagonal, which has two positions in a array.
Question 31 of 60 · Arrays from existing data · Medium
What are the shape and number of dimensions of np.array([[1, 2], [3, 4]])?
A
Shape (2, 2) and 2 dimensions
B
Shape (2,) and 2 dimensions
C
Shape (1, 4) and 2 dimensions
D
Shape (4,) and 1 dimension
Not quite. Try another option.
Answer: Shape (2, 2) and 2 dimensions
The nested sequence contains two rows with two elements each, forming a two-dimensional array.
Question 32 of 60 · Arrays from existing data · Medium
Consider a = np.array([1, 2, 3]), b = np.asarray(a), and then b[0] = 9. What is a afterward?
A
array([1, 2, 3])
B
array([9, 2, 3])
C
array([1, 9, 3])
D
array([9, 9, 9])
Not quite. Try another option.
Answer: array([9, 2, 3])
np.asarray usually returns the same array when no conversion is needed, so the mutation is shared.
Question 33 of 60 · Arrays from existing data · Medium
Consider a = np.array([1, 2, 3]), b = np.array(a, copy=True), and then b[1] = 8. What is a afterward?
A
array([1, 8, 3])
B
array([1, 2, 8])
C
array([8, 2, 3])
D
array([1, 2, 3])
Not quite. Try another option.
Answer: array([1, 2, 3])
copy=True creates independent storage, so changing b does not modify a.
Question 34 of 60 · Arrays from existing data · Medium
What does np.fromiter((x * x for x in range(4)), dtype=int) create?
A
array([0, 1, 4, 9])
B
array([0, 1, 2, 3])
C
array([0, 2, 4, 6])
D
array([1, 4, 9, 16])
Not quite. Try another option.
Answer: array([0, 1, 4, 9])
The generator yields the squares of 0, 1, 2, and 3, which fromiter stores in an array.
Question 35 of 60 · Arrays from existing data · Medium
What does np.fromstring("2 4 6 8", dtype=int, sep=" ") produce?
A
array([2, 4, 6])
B
array([2, 46, 8])
C
array([8, 6, 4, 2])
D
array([2, 4, 6, 8])
Not quite. Try another option.
Answer: array([2, 4, 6, 8])
The space separator causes NumPy to parse the string as four integer values.
Question 36 of 60 · Indexing and slicing · Medium
Let a = np.arange(12).reshape(3, 4). What does a[1:, ::2] return?
A
array([[4, 6], [8, 10]])
B
array([[0, 2], [4, 6]])
C
array([[5, 7], [9, 11]])
D
array([[4, 5], [8, 9]])
Not quite. Try another option.
Answer: array([[4, 6], [8, 10]])
1: selects the last two rows, while ::2 selects columns with indices 0 and 2.
Question 37 of 60 · Indexing and slicing · Medium
Given a = np.array([[10, 20, 30], [40, 50, 60]]), what is a[-1, -2]?
A
50
B
20
C
60
D
40
Not quite. Try another option.
Answer: 50
Index -1 selects the last row, and -2 selects the second-to-last element in that row.
Question 38 of 60 · Indexing and slicing · Medium
Consider a = np.array([1, 2, 3, 4]), b = a[1:3], and then b[0] = 9. What is a afterward?
A
array([1, 9, 9, 4])
B
array([9, 2, 3, 4])
C
array([1, 9, 3, 4])
D
array([1, 2, 3, 4])
Not quite. Try another option.
Answer: array([1, 9, 3, 4])
A basic NumPy slice is generally a view, so modifying b changes the corresponding element in a.
Question 39 of 60 · Indexing and slicing · Medium
What does a[(a > 2) & (a < 6)] return when a = np.array([1, 3, 5, 7])?
A
array([1, 7])
B
array([1, 3, 5])
C
array([3, 5])
D
array([3, 5, 7])
Not quite. Try another option.
Answer: array([3, 5])
The Boolean mask selects values that are both greater than 2 and less than 6.
Question 40 of 60 · Indexing and slicing · Medium
Let a = np.arange(9).reshape(3, 3). What does a[[2, 0], [1, 2]] return?
A
array([7, 0])
B
array([7, 2])
C
array([6, 2])
D
array([[7], [2]])
Not quite. Try another option.
Answer: array([7, 2])
Paired advanced indices select positions (2, 1) and (0, 2), whose values are 7 and 2.
Question 41 of 60 · Arrays vs lists · Hard
What are the final values of a and L?
a = np.array([1, 2, 3, 4])
v = a[1:3]
L = [1, 2, 3, 4]
s = L[1:3]
v[:] = 0
s[:] = [0, 0]
A
a is [1, 0, 0, 4]; L is [1, 0, 0, 4]
B
a is [1, 2, 3, 4]; L is [1, 0, 0, 4]
C
a is [1, 2, 3, 4]; L is [1, 2, 3, 4]
D
a is [1, 0, 0, 4]; L is [1, 2, 3, 4]
Not quite. Try another option.
Answer: a is [1, 0, 0, 4]; L is [1, 2, 3, 4]
A basic NumPy slice is a view, so changing v changes a. A list slice creates a new list, so changing s does not affect L.
Question 42 of 60 · Arrays vs lists · Hard
Given a = np.array([1, 2, 3]) and L = [1, 2, 3], what are the values of a * 2 and L * 2?
A
[2, 4, 6] and [1, 2, 3, 1, 2, 3]
B
[2, 4, 6] and [2, 4, 6]
C
[1, 2, 3, 1, 2, 3] and [1, 2, 3, 1, 2, 3]
D
[1, 2, 3, 1, 2, 3] and [2, 4, 6]
Not quite. Try another option.
Answer: [2, 4, 6] and [1, 2, 3, 1, 2, 3]
NumPy multiplication is element-wise, whereas multiplying a Python list by an integer repeats its elements.
Question 43 of 60 · Arrays vs lists · Hard
What are the final contents of a and L?
a = np.array([1, 2, 3], dtype=np.int64)
L = [1, 2, 3]
a[0] = 2.9
L[0] = 2.9
A
a is [2.9, 2, 3]; L is [2.9, 2, 3]
B
a is [3, 2, 3]; L is [2.9, 2, 3]
C
a is [2.9, 2, 3]; L is [2, 2, 3]
D
a is [2, 2, 3]; L is [2.9, 2, 3]
Not quite. Try another option.
Answer: a is [2, 2, 3]; L is [2.9, 2, 3]
The array has a fixed integer dtype, so 2.9 is truncated during assignment. A list can store values of different Python types.
Question 44 of 60 · Arrays vs lists · Hard
What do b and M contain after this code?
a = np.array([1, 2])
b = a
a += 10
L = [1, 2]
M = L
L = L + [10]
A
b is [1, 2]; M is [1, 2]
B
b is [11, 12]; M is [1, 2, 10]
C
b is [11, 12]; M is [1, 2]
D
b is [1, 2]; M is [1, 2, 10]
Not quite. Try another option.
Answer: b is [11, 12]; M is [1, 2]
a += 10 mutates the shared array in place. L + [10] creates a new list and rebinds L, leaving the list referenced by M unchanged.
Question 45 of 60 · Arrays vs lists · Hard
Consider a = np.array([[1], [2]]), b = np.array([10, 20, 30]), L = [[1], [2]], and M = [[10, 20, 30]]. Which pair gives a + b and L + M?
A
[[11], [12], [20], [30]] and [[1], [2], [10, 20, 30]]
B
[[11], [22], [30]] and [[11], [22], [30]]
C
[[11, 21, 31], [12, 22, 32]] and [[11, 21, 31], [12, 22, 32]]
D
[[11, 21, 31], [12, 22, 32]] and [[1], [2], [10, 20, 30]]
Not quite. Try another option.
Answer: [[11, 21, 31], [12, 22, 32]] and [[1], [2], [10, 20, 30]]
NumPy broadcasts shapes (2, 1) and (3,) to (2, 3). List addition concatenates the two outer lists.
Question 46 of 60 · Array creation routines · Hard
What array is created by np.arange(2, 14, 2).reshape(2, 3, order="F")?
A
[[2, 4, 10], [6, 8, 12]]
B
[[2, 6, 10], [4, 8, 12]]
C
[[2, 8, 10], [4, 6, 12]]
D
[[2, 4, 6], [8, 10, 12]]
Not quite. Try another option.
Answer: [[2, 6, 10], [4, 8, 12]]
arange creates [2, 4, 6, 8, 10, 12]. With Fortran ordering, reshape fills successive rows within each column.
Question 47 of 60 · Array creation routines · Hard
What does np.linspace(2, 10, num=4, endpoint=False, retstep=True) return?
A
The pair (array([2., 3.5, 5., 6.5]), 1.5)
B
The pair (array([2., 4.6667, 7.3333, 10.]), 2.6667)
C
The pair (array([2., 4., 6., 10.]), 2.0)
D
The pair (array([2., 4., 6., 8.]), 2.0)
Not quite. Try another option.
Answer: The pair (array([2., 4., 6., 8.]), 2.0)
Excluding the endpoint divides the interval of length into four equal steps, producing step size .
Question 48 of 60 · Array creation routines · Hard
Which array is produced by np.eye(3, 4, k=1, dtype=int)?
A
[[0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]
B
[[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0]]
C
[[0, 0, 0, 0], [1, 0, 0, 0], [0, 1, 0, 0]]
D
[[0, 0, 1, 0], [0, 0, 0, 1], [0, 0, 0, 0]]
Not quite. Try another option.
Answer: [[0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]
A positive offset k=1 places ones on the diagonal one column above the main diagonal.
Question 49 of 60 · Array creation routines · Hard
What are the value and dtype of np.full_like(np.array([1, 2, 3], dtype=np.int16), 3.8)?
A
[3.8, 3.8, 3.8] with dtype int16
B
[3.8, 3.8, 3.8] with dtype float64
C
[3, 3, 3] with dtype int16
D
[4, 4, 4] with dtype int16
Not quite. Try another option.
Answer: [3, 3, 3] with dtype int16
full_like adopts the prototype's dtype. Converting 3.8 to int16 truncates the fractional part.
Question 50 of 60 · Array creation routines · Hard
What is produced by np.fromfunction(lambda i, j: 10 * i + j, (2, 3), dtype=int)?
A
[[0, 10, 20], [1, 11, 21]]
B
[[0, 1, 2], [10, 11, 12]]
C
[[0, 1], [10, 11], [20, 21]]
D
[[0, 10, 20], [10, 20, 30]]
Not quite. Try another option.
Answer: [[0, 1, 2], [10, 11, 12]]
fromfunction passes coordinate arrays to the function. Here i identifies rows and j identifies columns.
Question 51 of 60 · Arrays from existing data · Hard
What are the final contents of a and c?
a = np.arange(4)
b = np.asarray(a)
c = np.array(a)
b[0] = 9
c[1] = 8
A
a is [0, 1, 2, 3]; c is [0, 8, 2, 3]
B
a is [9, 1, 2, 3]; c is [0, 8, 2, 3]
C
a is [9, 1, 2, 3]; c is [9, 8, 2, 3]
D
a is [9, 8, 2, 3]; c is [9, 8, 2, 3]
Not quite. Try another option.
Answer: a is [9, 1, 2, 3]; c is [0, 8, 2, 3]
asarray reuses an already compatible array, so b aliases a. By default, np.array(a) creates an independent copy.
Question 52 of 60 · Arrays from existing data · Hard
What does a.tolist() return after this code on any machine?
buf = bytearray([1, 0, 2, 0])
a = np.frombuffer(buf, dtype="<u2")
buf[0] = 3
A
[768, 512]
B
[3, 0, 2, 0]
C
[3, 2]
D
[1, 2]
Not quite. Try another option.
Answer: [3, 2]
frombuffer creates a view of the mutable buffer. Little-endian unsigned 16-bit groups decode [3, 0] as and [2, 0] as .
Question 53 of 60 · Arrays from existing data · Hard
What are a.tolist() and b.tolist()?
g = (i * i for i in range(5))
a = np.fromiter(g, dtype=int, count=3)
b = np.fromiter(g, dtype=int)
A
[0, 1, 4] and [4, 9, 16]
B
[0, 1, 4] and [0, 1, 4, 9, 16]
C
[0, 1, 4] and [9, 16]
D
[0, 1, 4, 9, 16] and []
Not quite. Try another option.
Answer: [0, 1, 4] and [9, 16]
The first call consumes exactly three generator values. The second call consumes the two remaining values.
Question 54 of 60 · Arrays from existing data · Hard
What are the inferred dtype and values of np.array([1, 2.5, True, 4 + 0j])?
A
object and [1, 2.5, True, (4+0j)]
B
int64 and [1, 2, 1, 4]
C
float64 and [1.0, 2.5, 1.0, 4.0]
D
complex128 and [(1+0j), (2.5+0j), (1+0j), (4+0j)]
Not quite. Try another option.
Answer: complex128 and [(1+0j), (2.5+0j), (1+0j), (4+0j)]
NumPy promotes all elements to a common dtype. The complex value causes integer, floating-point, and Boolean values to be promoted to complex numbers.
Question 55 of 60 · Arrays from existing data · Hard
What are the final contents of x and b?
x = np.array([[1, 2], [3, 4]], order="F")
a = np.asarray(x)
b = np.asarray(x, order="C")
a[0, 0] = 9
b[1, 1] = 8
A
x is [[1, 2], [3, 4]]; b is [[1, 2], [3, 8]]
B
x is [[9, 2], [3, 8]]; b is [[9, 2], [3, 8]]
C
x is [[9, 2], [3, 4]]; b is [[1, 2], [3, 8]]
D
x is [[9, 2], [3, 4]]; b is [[9, 2], [3, 8]]
Not quite. Try another option.
Answer: x is [[9, 2], [3, 4]]; b is [[1, 2], [3, 8]]
a aliases the compatible Fortran-contiguous array. Requesting C order requires b to be copied, so later changes to a and b are independent.
Question 56 of 60 · Indexing and slicing · Hard
For a = np.arange(12).reshape(3, 4), what does a[[0, 2], [1, 3]] return?
A
[[4, 8], [6, 10]]
B
[1, 3, 9, 11]
C
[[1, 3], [9, 11]]
D
[1, 11]
Not quite. Try another option.
Answer: [1, 11]
The two advanced index arrays are paired element by element, selecting coordinates (0, 1) and (2, 3).
Question 57 of 60 · Indexing and slicing · Hard
Let a = np.arange(24).reshape(2, 3, 4). What is a[:, [2, 0], 1:3]?
A
[[[9, 1], [10, 2]], [[21, 13], [22, 14]]]
B
[[[1, 2], [9, 10]], [[13, 14], [21, 22]]]
C
[[[9, 10], [1, 2]], [[21, 22], [13, 14]]]
D
[[[8, 9], [0, 1]], [[20, 21], [12, 13]]]
Not quite. Try another option.
Answer: [[[9, 10], [1, 2]], [[21, 22], [13, 14]]]
The second-axis index order is 2, 0, while 1:3 selects columns 1 and 2 from each chosen row.
Question 58 of 60 · Indexing and slicing · Hard
What are the final values of a and b?
a = np.arange(6).reshape(2, 3)
b = a[a % 2 == 0]
b[0] = 99
A
a is [[0, 1, 2], [3, 4, 5]]; b is [99, 2, 4]
B
a is [[99, 1, 2], [3, 4, 5]]; b is [0, 2, 4]
C
a is [[0, 1, 2], [3, 4, 5]]; b is [[99, 2, 4]]
D
a is [[99, 1, 2], [3, 4, 5]]; b is [99, 2, 4]
Not quite. Try another option.
Answer: a is [[0, 1, 2], [3, 4, 5]]; b is [99, 2, 4]
Boolean indexing returns a one-dimensional copy of the selected elements. Modifying that copy does not modify a.
Question 59 of 60 · Indexing and slicing · Hard
What is the final value of a?
a = np.zeros(3, dtype=int)
a[[0, 0, 1]] += 1
A
[1, 0, 1]
B
[1, 1, 0]
C
[2, 1, 0]
D
[2, 0, 1]
Not quite. Try another option.
Answer: [1, 1, 0]
Advanced indexing operates through a temporary array. Repeated index 0 is written back with the same incremented value rather than being incremented twice.
Question 60 of 60 · Indexing and slicing · Hard
What is the final value of a?
a = np.arange(5)
a[[1, 3]][0] = 99
A
[0, 1, 2, 99, 4]
B
[0, 99, 2, 3, 4]
C
[0, 1, 2, 3, 4]
D
[0, 99, 2, 99, 4]
Not quite. Try another option.
Answer: [0, 1, 2, 3, 4]
Integer-array indexing creates a copy. The assignment changes only the temporary result of a[[1, 3]], not the original array.