0
Basic Python question
What is the result of this code? nums = list(range(5)) print(nums[4]) --------------------------------- Hi guys, I wonder why the answer isn't "3"? Because my logic is : nums = list(range(5))------> [0,1,2,3,4] print(nums[4])--> print the forth number "3" , so the answer should be 3 . Please help to correct me ! Thanks !
4 odpowiedzi
+ 4
List index starts with 0
Nums = list(range(5))
So,
Nums = [0,1,2,3,4]
Nums[0] = 0
Nums[1] = 1
Nums[2] = 2
Nums[3] = 3
Nums[4] = 4
Hope it helps you!
0
sorry, I am still a bit confused.
I thought the list index start with 0 so that means:
The first index is 0, the second index is 1, the third is 2 , so the forth is 3....that's why I thought the answer is 3..
Please help to let me know which part went wrong QQ
0
Resa just as your nums = list(range(5)) goes 0 1 2 3 4 so does your print to the 4th 0 1 2 3 4<- if you said print(nums[0]) it would print 0 ...
0
Thank you everyone !! Finally I got it ahah ! :D