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 !

4th Jun 2020, 2:40 AM
Resa
Resa - avatar
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!
4th Jun 2020, 4:03 AM
Hacker Badshah
Hacker Badshah - avatar
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
4th Jun 2020, 3:01 AM
Resa
Resa - avatar
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 ...
4th Jun 2020, 3:32 AM
BroFar
BroFar - avatar
0
Thank you everyone !! Finally I got it ahah ! :D
4th Jun 2020, 6:39 AM
Resa
Resa - avatar