0

Explain how the following code outputs 8 List = [1,1,2,3,5,8,13] Print(list[list[4]])

The first question of the module 2 quiz is very confusing. Please explain it to me

30th Apr 2017, 12:46 AM
Joseph
3 Answers
+ 4
list=[1,1,2,3,5,8,13] print([list[list[4]]) that is the actual question. the answer is 8 because list[4] is the number 5.. so basically it is asking list[5] because that's what the inner index is returning. starting from 0, 8 is the #5 element in the list
30th Apr 2017, 1:00 AM
LordHill
LordHill - avatar
+ 2
Oh yes... I misscounted :P I think there's a typo mistake, and the example list should be [1,2,3,4,8,13] so ^^
30th Apr 2017, 12:56 AM
visph
visph - avatar
+ 1
The confusion comes with the fact that in computer, it's more logical to index items of lists ( arrays ) from zero instead from one. So, for a 3 items length list list = [ 1, 2, 3 ], the first element is 1, but it's index is 0... so you access it with list[0]. Conversely, list[n] access to the n+1th item: in your example, list[4] access to the 5th element of the list, wich is 8 ^^ For better getting the logical to count ( index ) from zero instead of one, think of stages of building: first level is the ground, 1st floor is the second level, and so on ;)
30th Apr 2017, 12:53 AM
visph
visph - avatar