>>> lst [1, 2, 3, (3, 'hj'), 9] >>> lst[3] (3, 'hj') >>> lst[4:4] [] >>> lst[4:5] [9] >>> lst[4:6] [9] >>> lst[6:7] [] >>> lst.insert(7,9) # 7 超过最大索引,不会真正插入在第八个位置,而是插入在尾部 >>> lst [1, 2, 3, (3, 'hj'), 9, 9] >>> lst.insert(100,'100th') >>> lst [1, 2, 3, (3, 'hj'), 9, 9, '100th'] >>> lst[100] # 单元素访问,超索引报错 Traceback (most recent call last): File "<pyshell#94>", line 1, in <module> lst[100] IndexError: list index out of range >>> lst[6] '100th' >>> lst[3:100] # 切片访问,超索引不会报错 [(3, 'hj'), 9, 9, '100th']
标签:100th,列表,hj,索引,lst,报错,100 From: https://www.cnblogs.com/sangern/p/17380333.html