+ 1
Which line of code will cause an error?
num = [5, 4, 3, [2], 1] print(num[0]) print(num[3][0]) #element indexed as [0] can be accessed when typed separately and with above line as well(altho in above line just element at [3] is accessed by the code but it doesnt throws any error whereas when above line is written with any other indexing other than [0] in [3][0] it throws and error print(num[5])
4 Réponses
+ 4
num[0] prints the first item in list
num[3][0]
First looks for item at 3rd index which is [2] and 2 is at index 0 so it prints 2 ,no error but if it was num[3][1] it would result in an error since that list at 3rd index includes only one element with index 0 i.e.[2]
num[5] means element in list num at index 5 but maximum index in list is 4 so it results in indexerror
0
Which line of code will cause an error?
num = [5, 4, 3, [2], 1]
print(num[0])
print(num[3][0])
print(num[5])
Answer:Line-4
0
Line-4
0
LINE 4