0

How does this come up with 8? How does is work?

list = [1,1,2,3,5,8,13,18] print(list(list[4])) It's print 8, but how??

14th Aug 2021, 3:06 PM
Prasert Chaichana
Prasert Chaichana - avatar
4 Answers
+ 4
Hi Prasert! Indices are always starting from zero in lists. This is the thing you have to keep in your mind when you're using lists to store data. So, list[4] is 5th element of this list that is 5. Now it becomes list([5]). Hence, list[5] is 8 which is the 6th element of this list.
14th Aug 2021, 3:18 PM
Python Learner
Python Learner - avatar
+ 2
It will get the element with the index 4 and make that the index in which is 5 Then will get the element with the index 5 That becomes 8
14th Aug 2021, 3:14 PM
Chris Jonathan
Chris Jonathan - avatar
+ 2
Your code throws an exception, as you've masked the original variable containing the reference to the actual list class. Try changing the parentheses into square brackets.
14th Aug 2021, 5:15 PM
Calvin Thomas
Calvin Thomas - avatar
0
Python Learner could not explain it better myself
14th Aug 2021, 3:40 PM
Chris Jonathan
Chris Jonathan - avatar