+ 2

Could someone clarify to me what this means?

I just can't wrap my head around the bottom code below. Could someone put this into separated steps and in plain English? I've managed to understand most of Python's lessons in control sturctures but the below code doesn't make sense to me. For some reason when I ran the code it gave me 8. list = [1, 1, 2, 3, 5, 8, 13] print(list[list[4]])

21st Jul 2017, 9:09 PM
Smith
Smith - avatar
4 Respuestas
+ 7
1) list is an array with 7 items [1,1,2,3,5,8,13] 2) index of an array is zero based, the first item has index 0, and so forth. ____________________________ index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | value | 1 | 1 | 2 | 3 | 5 | 8 | 13 | ____________________________ 3) as you can see the item with index 4 contains a value of 5, so list[4] gives you 5 4) list[list[4]] equals to list[5], refer to 3) 5) the value of element at index 5 is 8 Hope it's clear enough,
21st Jul 2017, 9:49 PM
Ipang
+ 4
First we have list[list[4]]. Anything that's deepest in the parenthesis is executed first. In this case, it's list[4]. According to the list above, list[4] = 5. Therefore, list[list[4]] = list[5] (since list[4] = 5.) Now we just have to figure out what list[5] is. That should be easy; It's 8. I hope this was easy to understand and that it helps!
21st Jul 2017, 11:24 PM
Erik Johanson
Erik Johanson - avatar
+ 3
list[list[4]] first of all, list[4] = 5 so if list[4] == 5 then list[list[4]] == list[5] = 8
21st Jul 2017, 9:24 PM
The Coding Sloth
The Coding Sloth - avatar
+ 2
Thank you so much Ipang that helped a lot.
21st Jul 2017, 10:21 PM
Smith
Smith - avatar