Why is `print("monty"[0:5])` not the same as `print("monty"[-5:-0])`?
I think I’ve cracked the whole idea how Python counts (see link below), but using a range and trying to call the last character using negative numbers still baffles me. http://matija.suklje.name/understanding-how-python-counts
why are you trying to reference the letters using negative numbers?
it seems to me to be an issue of counting m o n t y 0 1 2 3 4 5 if we introduce a backwards counting we must respect the fact that -0=0 is still counted for, yet the rule of the first character being excluded still applies. it is confusing because when accessing single characters, we count the starting point with negative numbers, i.e. 'monty'[-1]->'y' however when we count the second character in brackets is still excluded, as always 'monty'[-5:-1]->'mont' this could be like considered ordering from left to right, despite the fat we are using negative numbers m o n t y -5 -4 -3 -2 -1 again, 0 is out of bounds as it mixes positive and negative numerating the best we can do is 'monty'[-5:]->'monty' which may seem unsatisfactory, but makes sense in the respect I am referring to.
@TuringTest That is essentially how I understand it. Can you review my blog post in the first comment if my logic is correct?
I'm no expert, but I disagree with the idea that counting occurs between characters as is evidenced by your predicament with trying to start at 0=-0.
though this link would seem to contradict me and support you, so perhaps my explanation is not so great :S http://www.openbookproject.net/thinkcs/python/english2e/ch07.html
Well, I haven’t completely made up the whole idea. I’ve seen the slicing been done in more then one Python learning materials. I just never saw it expained in detail, using ranges and/or negative numbers. That’s what I’m trying to figure out. There *has* to be some logic behind it — it’s programming, it is only logic! I just don’t know whether I’ve got it or imagined a false logic around it…
It would make more sense to me to think that the way that indexing works is fundamentally defined differently for positive an negative counting | m | o | n | t | y | | 0 1 2 3 4 5 -6 -5 -4 -3 -2 -1 0<-there is no 0 place because 0 is already defined
mind you that whether indexing forwards or backwards for s[a:b] the second term b is not included, regardless of using positive or negative counting
@TuringTest True. That’s why `print("monty"[0:5])` is needed to print it all, even though `print("monty"[4])` is "y". But going one less with negative numbers can’t be less then -1 (well -0 which is 0, which is something else).
but it is one less than -1 because -1-1=-2 and 'monty'[-5:-1] prints up to 'monty'[-2] so it is the same
Em, one more. Sorry. How do you get then the last character? It should be -0, but it can’t be. (yes I know `[-5:]` works, but that’s not the question)
it can't be -0 because python recognizes -0 and 0 as the same object, and as I showed you that means that counting from -1 cannot include a 0 place.
That bit I understand. But what *is* the solution then? :p
Combine that with the rule that s[a:b] only prints up through s[b-1] and you come to the conclusion that 'monty'[-5:] is the only solution. I'm pretty sure that is the case as far as splicing goes
So I asked on irc://irc,freenode,net/#python and they said that “there is no endpoint that is a negative number that will include the last item in the sequence”, but that you can use `None` instead and it would work (which is essentially the same as ommitting it). OK, I guess that solves this question. Thanks for the help ☺
Interesting, thanks for the tip :)
Join our real-time social learning platform and learn together with your friends!