-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
Description
Bug report
Bug description:
Negative/reverse slicing (_variable[_one:_two:-1]) doesn't work this way.
According to the text,
_variable = [1, 2, 3, 4]
print(_variable[2:0:-1])
will print
[2, 1]
but actually, it prints
[3, 2]
(because stop index isn't included). Although it might have mentioned this previously, I still made this mistake when doing negative slicing.
One way to remember how slices work is to think of the indices as pointing between characters, with the left edge of the first character numbered 0. Then the right edge of the last character of a string of n characters has index n, for example:
+---+---+---+---+---+---+
| P | y | t | h | o | n |
+---+---+---+---+---+---+
0 1 2 3 4 5 6
-6 -5 -4 -3 -2 -1
The first row of numbers gives the position of the indices 0…6 in the string; the second row gives the corresponding negative indices. The slice from i to j consists of all characters between the edges labeled i and j, respectively.
Text
CPython versions tested on:
3.13
Operating systems tested on:
No response