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 counts 2 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[0])
>>> print(a[4])

The above explains just fine that print(a[0]) produces M and that if I want to get the last character (Y) I have to call print(a[4]).

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])
>>> print(a[0:4])
>>> print(a[-5])
>>> print(a[-1])

So, my therory is 3 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.

border slot border slot border slot border slot border slot border
0 1 2 3 4 5
-5 -4 -3 -2 -1 None

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])
>>> print(a[-4:-2])
>>> print(a[1:-2])

…the visual representation would be:

0 1 2 3 4 5
-5 -4 -3 -2 -1 None

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]
>>> a[-5:]

hook out → looking forward to week 2 of Mookie’s Python tutorial

Update: Added new finding that you cannot reach the last item in a sequence with negative numbers.

Update: My theory above is technically not true, but works quite well as an analogy nonetheless.

Update: Fixed a broken link. Thanks to Lydia Yates from Comparitech for catching it. BTW, they have a pretty cool list of some online courses on ethical hacking in Python

  1. 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. 

  2. I assume its the same in all computer languages, but I never got as far with others. 

  3. And if I am terribly wrong here – which is a possibility – please let me know. 

Related Posts





Stay in Touch