+ 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])

9th Jun 2020, 7:33 PM
Abhay Vishwakarma
Abhay Vishwakarma - avatar
4 Answers
+ 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
9th Jun 2020, 7:38 PM
Abhay
Abhay - avatar
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
15th Sep 2020, 7:05 AM
Divya Peddapalyam
Divya Peddapalyam - avatar
0
Line-4
26th Dec 2020, 5:50 AM
Ramukaved. K
Ramukaved. K - avatar
0
LINE 4
9th Nov 2022, 5:50 AM
kinzang namgay
kinzang namgay - avatar