- When you count the number of chairs in a room, you start counting from 1
- When you count the number of people ahead of you in a queue, you start counting from 1
Almost all things we count in real life, we start counting from 1. Then why do we start indexing from 0 and not 1 ?
Photo by albedo, some rights reserved
Simple answer
The simple answer to this question is :
It looks nice
đ
( As mentioned by Edsger W. Dijkstra )
Yes, he is the same Dijkstra who came up with the solution for the very famous travelling salesman problem, called âDijkstraâs algorithmâ that forms the foundation for the algorithms that you use on âGoogle Mapsâ and similar route based mobile Apps.
On a side-note, it was fun to read how he got the solution for this problem in just 20 minutes !!!
One morning I was shopping in Amsterdam with my young fiancée, and
tired, we sat down on the café terrace to drink a cup of coffee and
I was just thinking about whether I could do this, and I then
designed the algorithm for the shortest path. As I said, it was
a twenty-minute invention.
â Excerpts from Wikipedia
So, next time, when your fiancée calls you for shopping, you know what to do :D
The Python language reasoning
Interestingly enough, the reason for using a particular number for the first index is not the same for all languages. Infact, there are other languages, most notably like Matlab, that donât even use 0 for the first index. They use 1 to denote the first index and not 0.
When it comes to Python, indexing starts from 0. This makes perfect sense when we think of slicing. Using the slicing syntax,
if we want to fetch the first 3 elements, we would say
ex[:3]
If the indexing would have started from 1, to fetch the first 3 elements, we would have to use something ugly like
ex[1:4]
or
ex[:4]
Guido too speaks of a similar reasoning in his blog about why he decided to go with 0 indexing
The C language reasoning
The C language has its own reasons for indexing from zero. In C, an array variable is also a pointer to the first element of the array. And so, when you want to access the ânâth element in the array a
using a[n]
it internally gets converted to (a + n)
For the first element, the above logic would work elegantly, only if ânâ starts from 0
This answer on StackOverflow gives you a technically more detailed explanation
The Assembly code reasoning
This comment on Guidoâs blog post takes the discussion even further down to the hardware level. This implies that it was such a natural thing to do in the Assembly code
The explanation is pretty straight forward to understand. If we have to represent first 8 numbers in binary numbering system, we would choose binary numbers from 000 to 111 ( 0 to 7 ). Numbering starts from 0 !!! And we can do this using 3 bits.
If on the contrary, we had chosen to represent first 8 numbers as 1 to 8 in binary, then we would represent them in binary numbering system from 0001 to 1000 using 4 bits
So, in simple terms, starting from 0 saves us one extra bit.
The Mathematical reasoning
Coming back to why Djikstra called it âniceâ. You can read his hand written manuscript maintained by his colleagues in The University of Texas at Austin. It explains the mathematical reasoning behind choosing a zero index for mathematical operations. Mind you, this is not the same problem we have been discussing in Computer Science. But, it sort of alludes to the same reasoning.
You might also want to checkout a very well made video on this same article with some colourful visuals
In Mathematical terms, there are 2 problems that are being discussed. First is to figure out the numbers to denote a range of numbers. Should we include the start and end numbers or not ? If I want to represent numbers from 11, 12, 13, 14 then should I represent them as
numbers between 11 and 14
or
numbers between 11 and 15
or
numbers between 10 and 14
or
numbers between 10 and 15
?
And then once we have chosen the best option among the 4 options mentioned above, we now need to figure out if the first number should be indexed at 0 or 1
You can read the arguments in a more formal html document.
On a side note, this letter by Djikstra on why students should be introduced to programming using Haskell instead of Java shows how much he cared for his students