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]: []

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM