Someone who's been on Twitter recently asked me why Python uses an array index of 0 as the first bit (hereinafter referred to as 0-based) and lets me read an article on this topic (very interesting). This has caused me a lot of memories. One of the ancestors of the ABC language--python, using the 1-first index (hereinafter referred to as 1-based), and the C language, another language that has great influence on Python, uses the 0-based approach. I learned some of the earliest languages (Algol, Fortran, Pascal), some 1-based, some are not fixed. I think one of the reasons I decided to let Python use 0-based indexing is the python slicing (slice) syntax.
Let's take a look at the usage of slices first. Perhaps the most common use is to "cut out the top n bits from an array" or "cut n bits from the first bit of the number" (the former is actually a special use of the i== starting bit). If you use this syntax without the need to express an ugly +1 or-1 supplement, it would be very elegant.
Using the 0-based index, Python's semi-open interval slices and default match interval slicing syntax become very beautiful: a[:n] and A[i:i+n], the former standard notation is a[0:n].
If the 1-base is indexed, then you want a[:n] to be expressed as "fetch the top n elements" (this is not possible), you either use a closed interval slicing syntax, or use the slice start bit and slice length 2 parameters in the slice syntax. With 1-based indexing, half-open interval slicing syntax becomes less elegant. In this way, the closed interval slicing syntax is used, in order to express that you must write a[i:i+n-1] when taking n elements from the I-bit. Thus, if you use the 1-based index, it is more appropriate to use the slice start bit + length. So you can write a[i:n]. In fact, the ABC language is doing this-it uses a unique expression, written in A@i|n. (See Http://homepages.cwi.nl/~steven/abc/qr.html#EXPRESSIONS.)
But is index:length this way applicable in other cases? To tell you the truth, I'm a bit confused about this, but I think I was fascinated by the elegance of the half-open interval grammar. Especially when the two slice operation is adjacent, the end index value of the first slice operation is the start index value of the second slice, which is too beautiful to discard. For example, you want to cut an array into three parts with i,j two points-the three sections will be a[:i],a[i:j] and a[j:].
That's why I'm going to let Python use the 0-based indexing method.
English Original: Why Python uses 0-based indexing