Negative indexing means counting elements from the end of the array instead of the start. For example, -1 refers to the last element, -2 to the second last, and so on.
Click to reveal answer
beginner
How do you access the last element of a numpy array using negative indexing?
Use array[-1] to get the last element of the numpy array.
Click to reveal answer
beginner
What will array[-3] return in a numpy array?
It returns the third element from the end of the array.
Click to reveal answer
intermediate
Can negative indexing be used with slicing in numpy? Give an example.
Yes. For example, array[-3:-1] returns a slice starting from the third last element up to (but not including) the second last element.
Click to reveal answer
intermediate
Why is negative indexing useful in data science with numpy?
It allows easy access to elements from the end without needing to know the array length. This is helpful for tasks like getting recent data points or last values.
Click to reveal answer
What does array[-1] return in a numpy array?
AThe first element
BThe last element
CThe middle element
DAn error
✗ Incorrect
Negative index -1 always points to the last element in a numpy array.
If array = np.array([10, 20, 30, 40]), what is array[-2]?
A30
B20
C40
D10
✗ Incorrect
array[-2] accesses the second last element, which is 30.
Which slice uses negative indexing to get the last three elements of array?
Aarray[3:]
Barray[:3]
Carray[-3:]
Darray[:-3]
✗ Incorrect
array[-3:] slices from the third last element to the end.
What happens if you use array[-len(array)-1] in numpy?
AIndexError
BReturns the first element
CReturns the last element
DReturns None
✗ Incorrect
Negative indices less than -len(array) cause an IndexError.
Why might negative indexing be preferred over positive indexing?
AIt is faster
BIt avoids errors
CIt uses less memory
DIt allows easy access from the end without knowing length
✗ Incorrect
Negative indexing lets you access elements from the end without calculating array length.
Explain how negative indexing works in numpy arrays and give an example.
Think about how you count backwards from the last element.
You got /3 concepts.
Describe a real-life situation where negative indexing in numpy would be helpful.
Imagine you want the last few days of sales data.
You got /3 concepts.
Practice
(1/5)
1. What does negative indexing in a numpy array allow you to do?
easy
A. Sort the array in descending order
B. Access elements starting from the end of the array
C. Change the size of the array
D. Access elements only at the beginning of the array
Solution
Step 1: Understand negative indexing concept
Negative indexing means counting elements from the end of the array, starting at -1 for the last element.
Step 2: Compare options with concept
Only Access elements starting from the end of the array correctly describes accessing elements from the end using negative indexes.
Final Answer:
Access elements starting from the end of the array -> Option B
Quick Check:
Negative indexing = Access from end [OK]
Hint: Negative index -1 means last element [OK]
Common Mistakes:
Thinking negative index changes array size
Confusing negative indexing with sorting
Assuming negative index accesses start only at beginning
2. Which of the following is the correct syntax to get the last element of a numpy array arr using negative indexing?
easy
A. arr[0]
B. arr[1]
C. arr[-0]
D. arr[-1]
Solution
Step 1: Recall negative index for last element
In numpy, -1 is used to access the last element of an array.
Step 2: Check each option
arr[-1] uses arr[-1] which correctly accesses the last element. Others do not.
Final Answer:
arr[-1] -> Option D
Quick Check:
Last element = arr[-1] [OK]
Hint: Use arr[-1] for last element [OK]
Common Mistakes:
Using arr[-0] which is same as arr[0]
Using positive index 1 which is second element
Confusing zero index with negative zero
3. Given the numpy array arr = np.array([10, 20, 30, 40, 50]), what is the output of arr[-3]?
medium
A. 20
B. 40
C. 30
D. 50
Solution
Step 1: Understand negative index -3
Negative index -3 means counting from the end: last is -1 (50), second last -2 (40), third last -3 (30).
Step 2: Identify element at index -3
The element at arr[-3] is 30.
Final Answer:
30 -> Option C
Quick Check:
arr[-3] = 30 [OK]
Hint: Count backward: -1 last, -2 second last, -3 third last [OK]