+ 3
Why is the answer 0?
x = [True, 100, 1, 0, False] a = 3 b = -2 print(x[a] + x[b]) Thank you in advance!
5 Answers
+ 6
x[-2] returns the second last element of the list x.
So,
>>>x[3] + x[-2]
>>> 0 + 0
0
+ 3
x[3] and x[-2] are the same element of the list x, since a positive index counts from the beginning of the list, and a negative index counts from the end.
x[1] and x[-4] => 100
+ 2
-2 returns the second element counting from end.
Which is 0
0 +false(which is also 0)=0
+ 2
En python las listas comienzan desde cero:
0. 1. 2 3. 4
x = [True, 100, 1, 0, False]
-5 -4 -3 -2 -1
x [3] = 0
x [-2] = 0
0 + 0 = 0
0
This Program Output is 0 beacuse x is an Array and it's index is start from 0 and and ends with maxsize -1.
The index is either positive or Negative. If we put an index value which is Positive then it take the index from the Left Side and if we put a Negative value then it index is start from the Right side.
You put a variable a = 3 it means it is a positive value and the index is start from Left Side so the first index is 0 so the third index is 0 and b = -2 it means the index is start from the Right side so the first index is -1 so the -2 index is 0.
And you sum these two Variables
It means 0+0 =0 that's why the output is 0.