0
Can a Python person please explain this and provide a link that explains it
'number = 3 things = ["string", 0, [1, 2, number], 4.56] print(things[1]) print(things[2]) print(things[2][2])' Why does this last line produce a 3 >>>>>>>> print(things[2][2])
2 Answers
+ 7
Since number is a variable with value 3, the list things becomes
["string", 0, [1, 2, 3], 4.56]
things[2] is the element in this list at index 2. We have "string" at index 0, 0 at index 1, and [1, 2, 3] at index 2. So things[2] is the list [1, 2, 3].
Now things[2][2] is the element at index 2 in things[2], that is, in [1, 2, 3]. By the same argument, that must be 3, our answer.
Does that make sense?
+ 1
number = 3
things = ["string", 0, [1, 2, number], 4.56]
it evluate as
things = ["string", 0, [1, 2, 3], 4.56]
then
things[2][2]
address item in two levels:
. first search things for item 2 (indexed from 0) and get
[1, 2, 3]
. then search this result for item 2 and get
3