I started to learn Python again and this time I hope to have a good feeling about finishing it. This time instead of doing it alone, I joined “A Gentle Introduction to Python” MOOC-E (or Mookie, as I lovingly call it)1. After the first week it seems that it offers just the right flexibility and support to keep me going.
But that’s not the reason why I’m writing this post. I never understood how Python counts2 and I think I’ve cracked it now.
My brother (the physicist) explained to me that counting from 0 onwards is normal everywhere where serious mathematics is involved. OK, I understand that, it makes sense.
Let’s store a string with 5 characters and call its first and last character.
>>> a = "MONTY" >>> print(a) M >>> print(a) Y
The above explains just fine that
M and that if I want to get the last character (
Y) I have to call
But this logic is lacking if you try to understand why a 5 characters long string has a range of
print(a[0:5]) although, as we saw, the last character is № 4. And even less if you use negative numbers like
a(print[-3]). While in the same 5-character string the first character is № 0 and the last character is № 4; in the negative direction the last character is № -1 and the last character № -5.
>>> print(a[0:5]) MONTY >>> print(a[0:4]) MONT >>> print(a[-5]) M >>> print(a[-1]) Y
So, my therory is3 that the numbers don’t represent the drawers or slots in which the characters (or other items, objects) are placed, but the addresses from which on the data is to be read — the first border of each drawer, if you will. Also I think the data is always read in the positive direction. So even when using negative numbers to call objects, it will take the first one on the right of the address.
This is best represented in a table. Please note that the pipe characters (
|) are there only as a visual aid to represent borders.
At the same time as why negative numbers are different, this also explains how calling scquences works — again both using positive and negative numbers in sequences.
Using the above analogy, for the following code…
>>> print(a[1:3]) ON >>> print(a[-4:-2]) ON >>> print(a[1:-2]) ON
…the visual representation would be:
As you can see, this logic works for both positive and negative numbers as well as mixing the two in sequences.
The only issue that I still haven’t figured out is how do you call a sequence using negative numbers that includes the last character. By analogy the last limit should be -0, but it’s not ( probably due
-0 = 0). Just ommitting the second argument in the range works,
but it still baffles me as this is essentially the same as calling
None and it then reaches to the very end.
>>> print(a[-5:-0]) >>> a[-5:-1] 'MONT' >>> a[-5:] 'MONTY'
Update: added new finding that you cannot reach the last item in a sequence with negative numbers.
hook out → looking forward to week 2 of Mookie’s Python tutorial
- MOOC-E or Mechanical MOOC is a new concept of P2PU how to run a fully automated online course. “A Gentle Introduction to Python” is a course that uses videos and materials from MIT OpenCourseWare, the exercises on Codecademy and the OpenStudy platform as well as mailing lists for discussions. ↩
- I assume its the same in all computer languages, but I never got as far with others. ↩
- And if I am terribly wrong here — which is a possibility — please let me know. ↩