如果引用超出列表的边界范围,会出现“IndexError: list index out of range”,但在列表切片赋值时,却不会出现。例如:
lst = [1,2,3,4]
print(lst[5])
出现错误提示:
Traceback (most recent call last):
File "
IndexError: list index out of range
lst[5:]=['a','b','c']
print(lst)
[1, 2, 3, 4, 'a', 'b', 'c']
lst[-18:-16]=['-18','-16']
print(lst)
['-18', '-16', 1, 2, 3, 4, 'a', 'b', 'c']
这是好事还是坏事?