Reason why python index starts from zero | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Reason why python index starts from zero

Why does python use zero index?

2nd Feb 2020, 9:02 AM
Joseph Ojo
Joseph Ojo - avatar
16 Answers
+ 4
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.
2nd Feb 2020, 9:23 AM
Avinesh
Avinesh - avatar
+ 3
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)
2nd Feb 2020, 9:26 AM
Denise Roßberg
Denise Roßberg - avatar
+ 3
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.
2nd Feb 2020, 10:36 AM
HonFu
HonFu - avatar
+ 2
Are you asking for 'philosophical' reasons, like understanding the deeper reasoning, or are you having trouble practically dealing with it?
2nd Feb 2020, 10:16 AM
HonFu
HonFu - avatar
+ 2
Coder's Crux not all languages have array indices starting from 0. Some start them with 1.
3rd Feb 2020, 1:03 AM
Sonic
Sonic - avatar
+ 2
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.
3rd Feb 2020, 5:47 AM
N S
+ 1
Thanks Coder's Crux I did use the search bar, but the top results wasn't what I was looking for
2nd Feb 2020, 9:15 AM
Joseph Ojo
Joseph Ojo - avatar
+ 1
Joseph Ojo Can you explain which search was not helpful? Your own search or my search or both?
2nd Feb 2020, 9:28 AM
Denise Roßberg
Denise Roßberg - avatar
+ 1
HonFu philosophical reasons Someone asked me and I didn't have an answer except that python is zero indexed
2nd Feb 2020, 10:29 AM
Joseph Ojo
Joseph Ojo - avatar
+ 1
for every value of n is (n+1)
3rd Feb 2020, 10:14 AM
Code Jack
Code Jack - avatar
0
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.
2nd Feb 2020, 9:09 AM
coddy
coddy - avatar
0
When I searched, I used python zero indexed as keywords, so I was scrolling looking for answers with python as tags that's why I asked another question. Denise Roßberg , the same explanation applies, thanks
2nd Feb 2020, 9:32 AM
Joseph Ojo
Joseph Ojo - avatar
0
Joseph Ojo I used "index 0" / "index zero".
2nd Feb 2020, 9:44 AM
Denise Roßberg
Denise Roßberg - avatar
0
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.
4th Feb 2020, 5:03 AM
grdr