0

Python - How come printing outside a list range would NOT result in an error?

a = [1,2,3] print(a[2:5]) # outputs 3 Why did this not result in an error? There lacks slice positions 4 and 5...

4th Oct 2020, 2:07 AM
Solus
Solus - avatar
1 Antwort
+ 1
If you are slicing from the list it is okey to get out of range my guess is because even if there is nothing it will create an empty list, if you assign b = a[5:10] b will equal to [] but if you just take one index like a[5] it will be an error since there is no slicing to create a list and there is nothing on this index and since it is just a variable in this case integer so this integer is undefined and it will give you an error
4th Oct 2020, 2:18 AM
Ruba Kh
Ruba Kh - avatar