简体   繁体   中英

Why following code doesn't give “IndexError”?

I have following python code:

a = [0,1,2,3,4,5]
del(a[2:7])

Should not this give "IndexError"? If no then why?

This deletes the list slice from 2 to before 7. List slices do not throw index errors, rather if they extend beyond the end of the list, they return the whole remainder of the list.

>>> a = list(range(10))
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> a [5:20]
[5, 6, 7, 8, 9]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM