indexerror_解决IndexError:Python中的列表索引超出范围

  • Post author:
  • Post category:python


indexerror

Hello readers, welcome back to a yet another post of The Crazy Programmer. Today I’ll be discussing one of the common error programmers make when they start with programming and that is

IndexError

. Now before talking about this error, let’s try to see what an

Index

is and why this error occurs.

读者好,欢迎回到“疯狂程序员”的另一篇文章。 今天,我将讨论程序员在开始编程时遇到的常见错误之一,即

IndexError

。 现在,在讨论此错误之前,让我们尝试看看什么是

索引

以及为什么会发生此错误。

An index is a location of an item in an Array or a List and in most of the programming language, it starts from 0.


So when we count the elements in a list, we’ll count them from 1 but when we’ll try to access the elements of the list, we’ll start our index from 0.


Let us understand this with an example.

Please don’t skip the comments.

索引是项目在数组或列表中的位置,在大多数编程语言中,它从0开始。


因此,当我们计算列表中的元素时,我们将从1开始对其进行计数,但是当我们尝试要访问列表的元素,我们将从0开始索引。


让我们通过一个示例来理解这一点。

请不要跳过评论。