Most of the languages starts the index at zero. Please use the search bar next time to avoid duplicates. https://www.sololearn.com/discuss/1005710/?ref=app https://www.sololearn.com/discuss/521959/?ref=app https://www.sololearn.com/discuss/379907/?ref=app https://www.sololearn.com/discuss/785845/?ref=app https://www.sololearn.com/discuss/139443/?ref=app https://www.sololearn.com/discuss/91092/?ref=app https://www.sololearn.com/discuss/1663141/?ref=app
I had this question some time back. Now consider a programming language like C or C++ which closely work with memory. So whenever you pass an array as an argument, you only use the array name because it holds the memory address of the first element in the array. Now if I want to access the first element in the array then I cannot write *(array+1) because then it would give me the value of 2nd element in the array because the 'array' itself holds the 1st element address. So you have to write *(array+0) to get the first element. Hope it is clear now.
You can also look here: https://en.m.wikipedia.org/wiki/Zero-based_numbering And here is a list with programming languages. As you can see not all are zero-based: https://en.m.wikipedia.org/wiki/Comparison_of_programming_languages_(array)
My personal imagination: Every item of an array is not a point, but rather a 'width'. You can specify that: Like '8 bytes' or something. So every item (whatever the specific 'width') has a beginning and an end. I imagine a centimeter ruler. The first cm begins at zero, although it's the first. So if you want to find the beginning of the first cm/data item, you'd go to the zero. Or take time on a clock: First hour begins at 0, second at 1. When we say 02:05, we're already in the third hour. So it's also a bit of a convention (Python does it because the whole C clan does it), but you can find examples in real life.
Are you asking for 'philosophical' reasons, like understanding the deeper reasoning, or are you having trouble practically dealing with it?
As you already know, an array is a collection of elements of the same type stored in memory sequentially, so it has sense to think about indexes or 'positions'. Eg. When you write something like 'array' you are telling the interpreter: 'please, move on two positions to the right through the sequence and show me the content'. ps: I have choosen array as example but you can apply the same to any sequencial data type (string, list, etc).
Coder's Crux not all languages have array indices starting from 0. Some start them with 1.
It was the choice of programmer who programmed python. , if you wish to start with 1,2,19 or other number get start to devlop another language.
Thanks Coder's Crux I did use the search bar, but the top results wasn't what I was looking for
Joseph Ojo Can you explain which search was not helpful? Your own search or my search or both?
for every value of n is (n+1)
It's not only python, it is every language that the indexes start at zero. I can only assume it has something to do with memory management, related to the CPU but don't stress about it, I'm used to it by now.
Joseph Ojo I used "index 0" / "index zero".
the actual reason is that computers work binary at the very core and 2**0 = 1 (anything**0 equals 1). also it's sort of traditional because it was much more intuitive when considering how array pointers were used in older, low level programming languages.