Why numbering should start at zero?

Please note:

This has nothing to do with networking in particular! Not if you look from only one perspective. If you look from totally different perspective, with Cisco ACI and all other SDN solutions, you will probably meet with Python programming language (because you will) and then, somewhere in beginning of Python exploration this is the first question that will cross your mind. Of course, if you think like me!

Although not directly related to networking, the question bothered me for some time now and the answer is not only really logical when you read it but it is also given by a cool guy named prof.dr. Edsger W. Dijkstra

For almost every networking geek it will be enough to read through it.

Intro

After getting the CCIE I found the next thing I wanted to get my hands around. Python. As it seems, Cisco ACI fabric will be the next big thing in Cisco world. Other vendors all have something about SDN to offer these days. Most important part for us, networking engineers, is that they want us to give up the CLI and get us to use Python and REST calls to speak with our network equipment. Python was the logical next step.

I signed and passed Dr. Chuck’s Coursera Python class:

And got some books:

  • Python for Informatics by Charles Severance (Coursera Professor)
  • Learning Python, Fifth Edition by Mark Lutz
  • Python Pocket Reference, Fifth Edition by Mark Lutz
  • Fluent Python by Luciano Ramalho

After first few great lectures on Coursera by Dr.Chuck the question was born!

The Question

So my questions arose: “Why the hell in Python slices and range exclude the last item?”, “Why?”, “It’s not logical to me!”

The Answer

After going through some books while learning Python fundamentals I actually found the answer.  I found the reference to the answer by going through the book “Fluent Python” by Luciano Ramalho currently in early release by O’Reilly. The example below is stolen from that book to.

Python’s convention of excluding the last item in ranges and slices works great with zero-based indexing which is also used in Python.

Here are some benefits of this convention:

  • It shows the length of a range or slice when only the stop position is given: my_list[:6] and range(6) both produce six items.
  • It computes the length of a slice or range when start and stop are given by simply subtract start from stop.
  • It splits a sequence in two parts at any index x, without overlapping:
>>> l = [10, 20, 30, 40, 50, 60]
>>> l[:2]  # split at 2
[10, 20]
>>> l[2:]
[30, 40, 50, 60]
>>> l[:3]  # split at 3
[10, 20, 30]
>>> l[3:]
[40, 50, 60]

The Cool stuff

Prof.dr. Edsger W. Dijkstra wrote probably the best answer to this question using simply a pen, and the transcript together with the scan of the real handwriting can be fount here:

The original html and PDFs are found here:
https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html I just copied those on my server to be sure they are available as long as my page is..

 

Leave a Reply

%d bloggers like this: