What is the logic behind the first element of a list being number 'zero' and not 'one', as you would expect intuitively? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

What is the logic behind the first element of a list being number 'zero' and not 'one', as you would expect intuitively?

9th Jun 2017, 4:41 PM
Olivier Casse
Olivier Casse - avatar
6 Answers
+ 7
Very interesting question @Olivier "thumbs up". I found an interesting article on this query at https://www.quora.com/Why-do-array-indexes-start-with-0-zero-in-many-programming-languages
9th Jun 2017, 4:45 PM
देवेंद्र महाजन (Devender)
देवेंद्र महाजन (Devender) - avatar
+ 7
It all comes back to how C (and BCPL before it) handled arrays. Imagine the memory in your computer as a big, numbered list - any variable you use will live somewhere in this list and will have a memory address (so the computer knows where to look up the value of your variable). Now, if you ask the computer to give you space for an array with 10 elements, it'll just give you a block of memory, saying for example "here, address 500 to 509 are yours" and you can look up any element in that array by simply adding to your offset of 500. In C-like code: array = make_array(); // points at address 500. pointer_to_second_element = array + 1; // points at address 501. To look up the value of a memory address, C had the `*` operator, and what you know as "array lookup" with `array[i]` is really just a shorthand. *(array) == array[0] == first element (stored at memory location 500) *(array + 1) == array[1] == second element (stored at memory location 501) *(array + 2) == array[2] == third element (stored at memory location 502) and so on. Since there is little point in switching, we still do it this way in almost all languages. I hope that made sense! PS: Funny thing in C: array[0] and 0[array] do the same, because of what I wrote above :P
9th Jun 2017, 5:13 PM
Schindlabua
Schindlabua - avatar
+ 4
For computer hardware address, 0 is the starting address, not 1
9th Jun 2017, 4:44 PM
Calviղ
Calviղ - avatar
+ 4
Some people thought the same and went even farther - they started to count from 1, not 0. Those people invented R and Matlab ;)
9th Jun 2017, 10:02 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 1
Thanks for your answers. I hope my little summary below and comments are readable. All right, to sum up, this is inherited from programming practices -the harddrive address = memory block start + ...zero for the first element. As a consequence range(n) includes zero and excludes n. This is the second important notion: containing n+1 elements instead of n would be 'ugly'. However, as one progresses through the course one notices that this is not the case with strings (and dictionnaires, obviously, although they may be "enumerated". Any comments on that?
6th Jul 2017, 12:23 PM
Olivier Casse
Olivier Casse - avatar
0
indices are offsets, meaning 'how far away'. A list or an array (or a tuple, or any 'collection') has a 'beginning' or a first location (address). the first item is 0 locations from the beginning. The next item is 1 location from the beginning, and so on.
31st Oct 2017, 9:12 AM
peter