python切片取值和下標取值時,超出範圍怎麼辦?

可迭代對象下標取值超出索引範圍,會報錯:IndexErrorpython

可迭代切片取值超出索引範圍,不報錯,而是返回對應的空值.shell

 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]: []
相關文章
相關標籤/搜索