The founder of the Python language explains why Python arrays are indexed from 0

巴扎黑
Release: 2017-04-30 15:53:36
Original
2538 people have browsed it

Someone recently asked me on Twitter why Python uses 0-based array indexing (hereinafter referred to as 0-based), and asked me to read an article on this topic (very interesting). This brought back a lot of memories for me. ABC language, one of the ancestors of Python, uses a 1-based indexing method (hereinafter referred to as 1-based), while C language, another language that has a huge influence on Python, uses 0-based Way. Some of the first languages ​​I learned (Algol, Fortran, Pascal) were 1-based and some were fluid. I think one of the reasons why I decided to use 0-based indexing in Python was Python's slice syntax.

Let’s first look at how to use slices. Perhaps the most common usage is "cut out the first n bits from the array" or "cut out n bits from the i-th bit of the value" (the former is actually a special use of i==the starting bit). It would be very elegant if this syntax didn't need to be expressed in ugly +1 or -1 complements.

Using 0-based indexing, Python's half-open interval slicing and default matching interval slicing syntax becomes very beautiful: a[:n] and a[i:i+n], the former's standard writing is a[0: n].

If it is a 1-base indexing method, then if you want a[:n] to be expressed as "take the first n elements" (this is not possible), you must either use a closed interval slicing syntax, or use it in the slicing syntax The form of two parameters: slice starting bit and slice length. Using 1-based indexing, the half-open interval slicing syntax becomes inelegant. In this way, closed interval slicing syntax is used. In order to express taking n elements from the i-th position, you must write a[i:i+n-1]. From this point of view, if you use a 1-based index, it is more appropriate to use the form of slice starting bit + length. This way you can write a[i:n]. In fact, the ABC language does this - it uses a unique expression, written a@i|n. (See http://homepages.cwi.nl/~steven/abc/qr.html#EXPRESSIONS.

​But, does the index:length method apply in other situations? To be honest, I don't remember much about this, but I think I was fascinated by the elegance of semi-open interval syntax. Especially when the two slicing operations are adjacent and the end index value of the first slicing operation is the starting index value of the second slice, it is too beautiful to discard. For example, you want to cut an array into three parts at two points i and j - the three parts will be a[:i], a[i:j] and a[j:].

This is why I want Python to use 0-based indexing.

English original text: why Python uses 0-based indexing

The above is the detailed content of The founder of the Python language explains why Python arrays are indexed from 0. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template