I'm not comprehending this code, help please? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I'm not comprehending this code, help please?

number = 3 things = ["string", 0, [1, 2, number], 4.56] print(things[1]) print(things[2]) print(things[2][2]) that last line is supposed to be 3 but im not getting how that works out.

16th Apr 2017, 5:22 PM
Stephen Zimmerman
Stephen Zimmerman - avatar
4 Answers
+ 14
The output I get when I run your code is: 0 [1,2,3] 3 So the last line is 3. Is that not what you were expecting?
16th Apr 2017, 5:25 PM
Michael Foster
Michael Foster - avatar
+ 14
Remember the second list is nested so Python counts it like this: 0 ="string" 1= 0 2 = the nested list Then: 0 = 1 1 = 2 2 = number
16th Apr 2017, 5:39 PM
Michael Foster
Michael Foster - avatar
+ 1
yes I just forgot about the nested part of it, this is my first language :) thanks!
16th Apr 2017, 5:46 PM
Stephen Zimmerman
Stephen Zimmerman - avatar
- 1
print(things[2]) is [1,2,number] and the index 2 of that list is number.
16th Apr 2017, 5:39 PM
merkrafter