+ 1

Can anyone explain this code briefly?

https://code.sololearn.com/cS8RMe8b7Vm5/?ref=app

19th Dec 2021, 3:12 AM
Santhos RAJ 14
3 Answers
+ 4
A short piece of code, yet a bit hard to understand ryt. a=[5,4,3,2,2,2,1] print ( a[ a[ a[ a[2]+1 ] ] ]) First it gets the value of second index, which is 3. Then adds '1' to it. So it is, 4. 3+1=4 Then it gets the 4th index from the array, the value is 2. Then it gets the 2nd index from the array, the value is 3. Then it gets the value of 3rd index, which is 2. The final answer is 2.
19th Dec 2021, 3:30 AM
Steve Sajeev
Steve Sajeev - avatar
+ 1
Steve Sajeev , thank you so much!
19th Dec 2021, 4:45 AM
Santhos RAJ 14
0
Given that the list 'a' contains [5,4,3,2,2,2,1] The print statement contains a[a[a[a[2]+1]]] The innermost square brackets will be evaluated first which is "a[2]" a[2] = 3 And then it's "a[2]+1" = 3+1 =4 The second-most inner square bracket is a[a[2]+1] = a[4] = 2 The next inner bracket is a[a[a[2]+1] ] = a[2] = 3 The outermost bracket is a[a[a[a[2]+1] ]] = a[3] = 2 The result is the third element of the list which is true
20th Dec 2021, 5:26 PM
Shahana.S