Recall & Review
beginner
What is negative indexing in numpy arrays?
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?
✗ 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]?
✗ Incorrect
array[-2] accesses the second last element, which is 30.
Which slice uses negative indexing to get the last three elements of array?
✗ Incorrect
array[-3:] slices from the third last element to the end.
What happens if you use array[-len(array)-1] in numpy?
✗ Incorrect
Negative indices less than -len(array) cause an IndexError.
Why might negative indexing be preferred over positive indexing?
✗ 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.