Why python start index position at 1 when backward

Refresh

April 2019

Views

41 time

3

Why python start index from 1 instead of 0 when counting array backward.

list = ["a", "b", "c", "d"]
print(list[3]) # Number 3 prints "d"

print(list[-4]) # Number -4 prints "a"

3 answers

2

Because -0 in Python is 0.
With 0 you get first element of list and
with -1 you get the last element of the list.

list = ["a", "b", "c", "d"]
print(list[0]) # "a"
print(list[-1]) # "d"
11

explain it in another way, because -0 is equal to 0, if backward starts from 0, it is ambiguous to interpreter.


if you are confused about -, and looking for another way to index backward more understandable, you can try ~, it is a mirror of forward:

list = ["a", "b", "c", "d"]
print(list[~0])   # d
print(list[~1])   # c
9
list[-1]

Is short hand for:

list[len(list)-1]

The len(list) part is implicit. That's why the -1 is the last element. That goes for any negative index - the subtraction from len(list) is always implicit