Zero based indexing

python
Published

October 22, 2014

Reads

Comment

For Europeans zero based indexing feels reasonable if we think of floors in a house, the lowest floor is ground floor, then 1st floor and so on.

A house with 2 stories has ground and 1st floor. It is natural in this way to index zero-based and to count 1-based.

What about slicing instead? This is a separate issue from indexing. The main problem here is that if you include the upper bound then you cannot express the empty slice. Also it is elegant to print the first n elements as a[:n]. Slicing a[i:j] excludes the upper bound, so it probably easier to understand if we express it as a[i:i+n].