I need help with some python code. And someone please explain to me how the output is 13 [solved] | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I need help with some python code. And someone please explain to me how the output is 13 [solved]

x=[6, 4, 2, 9] x=x[::-1] print(x[0]+x[2])

31st Aug 2021, 4:13 PM
McAlvin
McAlvin - avatar
4 Answers
+ 9
The second line reverses the list thus x becomes [9,2,4,6] The value of x[0] is 9 while x[2] is 4 so their sum is 13
31st Aug 2021, 4:17 PM
Ahmad
Ahmad - avatar
+ 5
::-1 basically reverses the list, so to see the indexes, count backwards. 9+4=13, so that's how you get the output.
31st Aug 2021, 4:20 PM
Chloe
Chloe - avatar
+ 3
x=[6, 4, 2, 9] x=x[::-1] #now x is reversed -> x=[9, 2, 4, 6] print(x[0]+x[2]) #first index + third index #9 + 4 #= 13
31st Aug 2021, 5:29 PM
Sousou
Sousou - avatar
+ 1
ans is 13
21st Jan 2023, 5:21 PM
Engr.Ayesha Liaqat
Engr.Ayesha Liaqat - avatar