Recently, someone asked me on Twitter why Python uses the array index with 0 as the first (hereinafter referred to as 0-based ), let me read an article on this topic (very interesting ). This has aroused many memories of me. ABC language-one of the ancestors of Python, uses the index method (1-based for short) with 1 as the first, while C language-another language that has a huge impact on Python, use the 0-based method. Some of the first languages I learned (Algol, Fortran, Pascal) are 1-based and some are not fixed. In my opinion, the reason why I decided to allow Python to use 0-bas is that someone recently asked me on Twitter why Python uses the array index method with 0 as the first (hereinafter referred to as 0-based ), let me read an article on this topic (very interesting ). This has aroused many memories of me. ABC language-one of the ancestors of Python, uses the index method (1-based for short) with 1 as the first, while C language-another language that has a huge impact on Python, use the 0-based method. Some of the first languages I learned (Algol, Fortran, Pascal) are 1-based and some are not fixed. I think one of the reasons why I decided to use the 0-based index method for Python is the slice syntax of Python.
Let's take a look at the slice usage first. The most common usage is to cut out the first n bits from the array or cut out n bits from the I bits of the number. (the former is actually a special usage of I = start bits. ). If you do not need to add an ugly + 1 or-1 complement to the table when using this syntax, it will be very elegant.
Using the 0-based index method, Python's semi-open interval slicing and the default matching interval slicing syntax become very beautiful: a [: n] and a [I: I + n], the standard expression of the former is a [0: n].
If the index is 1-base, you want to make the [: n] table "obtain the first n elements" (this is not acceptable ), you can either use a closed interval slicing syntax or use the slice start bit and slice length parameters in the slice syntax. Using the 1-based index method, the semi-open interval slicing syntax becomes not elegant. In this way, the closed interval slicing syntax is used. to express the n elements obtained from the I-bit, you must write a [I: I + n-1]. In this case, if a 1-based index is used, it is more appropriate to use the slice start position + length. In this way, you can write a [I: n]. In fact, the ABC language does this-it uses a unique expression, written as a @ I | n. (See http://homepages.cwi.nl /~ Steven/abc/qr.html # EXPRESSIONS.
However, is the index: length method applicable in other cases? To be honest, I cannot remember this, but I think I am fascinated by the elegance of the semi-open interval syntax. Especially when the positions of the two slices are adjacent, the index value at the end of the first slice operation is the index value at the starting point of the second slice, which is too beautiful to be discarded. For example, if you want to split an array into three parts by using the I and j vertices, the three parts will be a [: I], a [I: j] and a [j:].
This is why I want Python to use a 0-based index.
Why Python uses 0-based indexing
The above is the explanation of why the index of the Python array starts from 0. For more information, see other related articles in the first PHP community!