I am new to python. The code below returns 8. Why?
numbers = [2,3,5,8]
print(numbers[numbers[1]])
When working with slicers, I would expect to see:
print(numbers[:]) = [2,3,5,8]
or
print(numbers[:-1]) = [2,3,5]
I have never seen something like this:
print(numbers[numbers[1]])
Is the list numbers slicing itself? And if yes, how is it doing it?
>Solution :
What is happening is just nested indexing.
It’s not what you think is happening, instead it’s actually very trivial.
numbers[1] = 3 (for the numbers = [2,3,5,8] )
is acting as an index to the outer one
number[number[1]] = number[3] = 8