Why does Python return negative list indexes?

后端 未结 4 748
攒了一身酷
攒了一身酷 2020-11-28 16:03

If I have this list with 10 elements:

>>> l = [1,2,3,4,5,6,7,8,9,0]

Why will l[10] return an IndexError, but l[-1] returns 0?

4条回答
  •  佛祖请我去吃肉
    2020-11-28 16:29

    Because, index value starts from 0. In your case the last index value is 9, no more index value after 9. when you try print the values beyond the index value limit, it must throws an error

提交回复
热议问题