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
✗ Incorrect
NumPy arrays use zero-based indexing, so arr[0] returns the first 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>
✗ Incorrect
Indexing starts at 0, so third row is index 2 and first column is index 0.
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
✗ Incorrect
Accessing an index outside the array size raises an IndexError.
What does arr[-2] return in a NumPy array?
ASecond element
BSecond last element
CLast element
DAn error
✗ Incorrect
Negative indices count from the end, so -2 is the second last element.
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
✗ Incorrect
You can use either comma-separated indices or chained brackets to access elements in multi-dimensional arrays.
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.
Practice
(1/5)
1. What does single element access in a numpy array allow you to do?
easy
A. Sort the entire array
B. Add new rows to the array
C. Read or change one specific value in the array
D. Calculate the sum of all elements
Solution
Step 1: Understand the concept of single element access
Single element access means selecting one value from the array using its position.
Step 2: Identify what single element access can do
It allows reading or changing that one value, not adding rows, sorting, or summing.
Final Answer:
Read or change one specific value in the array -> Option C
Quick Check:
Single element access = read/change one value [OK]
Hint: Single element access targets one value by position [OK]
Common Mistakes:
Confusing single element access with adding or removing elements
Thinking it can perform operations like sum or sort
Mixing up element access with slicing
2. Which of the following is the correct syntax to access the element in the 2nd row and 3rd column of a numpy array named arr?
easy
A. arr[2, 3]
B. arr[1, 2]
C. arr(1, 2)
D. arr[2][3]
Solution
Step 1: Recall zero-based indexing in numpy
Indexing starts at 0, so 2nd row is index 1, 3rd column is index 2.
Step 2: Check syntax for multi-dimensional access
Use square brackets with comma-separated indexes: arr[1, 2]. Parentheses or double brackets are incorrect here.