I am a bit confused on the Intro to NumPy notebook. In the Initial intro for slicing the notes state that if no value is given for end, the default is the length of the array - 1. This is not true, the default value is the length of the array. In NumPy slicing stops one element short of end, so if the default for end was length - 1 then example_arr[0:] would be missing the last element of example_arr. This is not the case when experimenting, can someone please clarify this or fix the notebook?
@AdrianG recall in computing, arrays start at index 0, not 1.
So if you have 100 numbers, you’re going from index 0 to 99.
@Nevermnd I see that, but since the end parameter of slicing in NumPy is not inclusive of the supplied end index, supplying (len - 1) as end would actually give us the (len - 2)th index, would it not?
Perhaps there is an error in the notes ?
example_arr[0:] or example_arr[:] should give you the complete array. The only case it would be different would be if you did something like example_arr[0:-1].
My thoughts exactly, and the docs for NumPy seem to suggest the same thing, that the default for end is the length of the array so that the last element given is example_arr[len-1]. Is there a way to suggest edits to the notes to make this correction?
@TMosh I don’t know if you have any influence/input to add here. I don’t have access to the course.
I don’t think any correction is needed. The note as you stated it is correct. If I have an array of length 4 (meaning it has 4 elements), then the last element of that array is:
myArray[3]
That is because of the key point that Anthony made in his first reply: indexing in python is “0 based”. That is a choice and not everyone makes the choices the same way: in MATLAB it is “1 based”. But we are doing python here, so we don’t have a choice.
@paulinpaloalto
That isn’t true, I created a quick test for this:
As we can see here, the length of the example array is 5, which means according to the notes the default end should be 5 - 1 = 4, but when I manually slice it passing in 4 as the end value it omits the final value.
This is not because of the 0-based counting that we do in Python, but instead because of the behavior of slicing in NumPy. Slicing in NumPy is exclusive of the value supplied for end. As seen above, when I pass in the value 4 for end
the last value given is actually example_arr[3]
, or stated another way the last value for a slice is example_arr[end - 1]
so if the notes were correct and the default value is the array length minus 1, the last value seen in the default slice would be example_arr[length - 1 - 1]
. That is not the case as seen above.
Sorry, I should have gone and looked at the notebook before responding. I agree that the text as written is incorrect. I have access to the Git repo for this course and am in the middle of filing a bug about this.
I think what they were trying to say was to make the point about what the actual last element of the slice would be in terms of the index value. But the way they phrased it was as the actual end
argument value and, exactly as you say, that is wrong.
Thanks for reporting the issue!
No worries at all, the 0-based counting is certainly important to know it just confused me since the notes disagreed with experimentation. Thank you for the replies and filing a bug for this change!