Q: l[10] IndexError, l[-1] 0?
A. Python ( ) . , 0.
l = [1,2,3,4,5,6,7,8,9,0]
10 . 0, 9. 10, Python IndexError, , .
Python also uses a negative index convention to access items from the "end" of a list or sequence. The index value -1indicates the last item in the list, -2following the last, etc. Since the last item on your list 0, this is what returns l[-1].
@Lattyware's answer already shows you how to throw / throw an exception, I hope this answers your original question.
Levon source
share