List question? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

List question?

list = [1, 1, 2, 3, 5, 8, 13] print(list[list[4]]) answer is "8" how ?????????

20th Aug 2018, 10:56 AM
Parveen Kumar
Parveen Kumar - avatar
3 Answers
+ 3
list=[1,1,2,3,5,8,13] print(list[list[4]]) | list[4]=5 | print(list[5])=8 where list[4] is replace by 5 because list[4] is 5 that's why you got 8.
20th Aug 2018, 11:02 AM
Maninder $ingh
Maninder $ingh - avatar
+ 3
Parveen Kumar The inner part of the expression 'list[4]' results in 5. Now you have the expression print(list[5]). Then 'list[5]' results in 8. Here you have print(8). Array are zero-based meaning that the index of the first element is 0, rather than 1.
20th Aug 2018, 11:01 AM
Steppenwolf
Steppenwolf - avatar