python切片取值和下标取值时,超出范围怎么办?

可迭代对象下标取值超出索引范围,会报错:IndexError

可迭代切片取值超出索引范围,不报错,而是返回对应的空值.

 1 a=[1,2,3,4]
 2 a[99]
 3 Traceback (most recent call last):
 4   File "/usr/lib/python3/dist-packages/IPython/core/interactiveshell.py", line 2882, in run_code
 5     exec(code_obj, self.user_global_ns, self.user_ns)
 6   File "<ipython-input-32-f44cd5f2f9f9>", line 1, in <module>
 7     a[99]
 8 IndexError: list index out of range
 9 
10 
11 a[99:]
12 Out[33]: []

猜你喜欢

转载自www.cnblogs.com/zywscq/p/10760242.html