Hay fellow programmers,
I'm new to programming and was wondering if someone could help clear something up for me? I'm not sure if it's because of the way it's written or if it is simply a real, but contradictory way of doing something.
I'm currently learning Python from the book 'Learn Python in One Day' and their explanation of list slicing has confused me. At the beginning, they state individual indexes always start from zero, but in a later example they seem to be using '1' as the starting point(?) and ending at index 5 - 1?
The example they're using for below is: userAge = [21, 22, 23, 24, 25]
At the beginning, in the extract below, they explain that the index for 21 = 0, the index for 22 = 1, 23 = 2, 24 = 3, and 25 = 4.
But when they get to the bottom of the extract they are talking about a 5th index (despite there not being one in the example above)...
Here's the extract from the book. I've highlighted the bits that have confusticated me:
"Individual values in the list are accessible by their indexes, and indexes always start from ZERO, not 1. This is a common practice in almost all programming languages, such as C and Java. Hence the first value has an index of 0, the next has an index of 1 and so forth. For instance, userAge[0] = 21, userAge[1] = 22.
Alternatively, you can access the values of a list from the back. The last item in the list has an index of -1, the second last has an index of -2 and so forth. Hence, userAge[-1] = 25, userAge[-2] = 24.
You can assign a list, or part of it, to a variable. If you write userAge2 = userAge, the variable userAge2 becomes [21, 22, 23, 24, 25].
If you write userAge3 = userAge[2:4], you are assigning items with index 2 to index 4-1 from the list userAge to the list userAge3. In other words, userAge3 = [23, 24].
The notation 2:4 is known as a slice. Whenever we use the slice notation in Python, the item at the start index is always included, but the item at the end is always excluded. Hence the notation 2:4 refers to items from index 2 to index 4-1 (i.e. index 3), which is why userAge3 = [23, 24] and not [23, 24, 25].
The slice notation includes a third number known as the stepper. If we write userAge4 = userAge[1:5:2], we will get a sub list consisting of every second number from index 1 to index 5-1 because the stepper is 2. Hence, userAge4 = [22, 24].
In addition, slice notations have useful defaults. The default for the first number is zero, and the default for the second number is size of the list being sliced. For instance, userAge[ :4] gives you values from index 0 to index 4-1 while userAge[1: ] gives you values from index 1 to index 5-1 (since the size of userAge is 5, i.e. userAge has 5 items)."
I'm not sure where they are getting the '5 - 1' if the original example had only index 0 - 4 (despite that meaning there are five items in the list). Is this a "rule change" or have they changed the index numbers from 0 to 4, to 1 to 5? I know the '5 - 1' means you would go back to index 4 due to the "end exclusive" they mention above, but don't you need the item there before you can minus it? In the original example, there is no 5th index (though there are 5 items on the list)...
Needless to say, I am very confused! Haha.
Thanks in advance for your help!
Jane. x