0
0
NumPydata~5 mins

Single element access in NumPy - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
How do you access a single element in a NumPy array?
You use square brackets with the index of the element, like array[index]. For multi-dimensional arrays, use commas to separate indices, like array[row, column].
Click to reveal answer
beginner
What is the index of the first element in a NumPy array?
The first element has index 0. NumPy arrays use zero-based indexing, so counting starts at 0.
Click to reveal answer
beginner
How do you access the element in the second row and third column of a 2D NumPy array named arr?
Use arr[1, 2]. Remember, counting starts at 0, so row 1 is the second row and column 2 is the third column.
Click to reveal answer
beginner
What happens if you try to access an index that is out of range in a NumPy array?
NumPy will raise an IndexError because the index does not exist in the array.
Click to reveal answer
beginner
Can you use negative indices to access elements in a NumPy array? What does array[-1] return?
Yes, negative indices count from the end. array[-1] returns the last element of the array.
Click to reveal answer
What does arr[0] return for a NumPy array arr?
AThe first element
BThe last element
CAn error
DThe middle element
How do you access the element in the third row and first column of a 2D NumPy array matrix?
A<code>matrix[0, 2]</code>
B<code>matrix[3, 1]</code>
C<code>matrix[1, 2]</code>
D<code>matrix[2, 0]</code>
What will happen if you try to access arr[10] when arr has only 5 elements?
ARaises an <code>IndexError</code>
BReturns the last element
CReturns <code>None</code>
DReturns the first element
What does arr[-2] return in a NumPy array?
ASecond element
BSecond last element
CLast element
DAn error
Which of these is the correct way to access a single element in a 3D NumPy array arr?
A<code>arr[1, 2, 3]</code>
B<code>arr[1][2][3]</code>
CBoth A and B
DNeither A nor B
Explain how to access a single element in a 2D NumPy array and what indexing rules apply.
Think about how rows and columns are counted starting from zero.
You got /3 concepts.
    Describe what happens if you try to access an element outside the bounds of a NumPy array.
    What error does Python give when you ask for something that does not exist?
    You got /3 concepts.