0

Nums = list.range(5) print(nums[4]) Why nums[4] == 4?

22nd Dec 2016, 2:16 AM
Kenzhebekov Nurtileu
Kenzhebekov Nurtileu - avatar
4 Respostas
+ 5
Nums = list.range(5)>>>>>>you created a list that is in the range 0-4(remember the range function always starts at 0 and ends at the number less that the number you put)...so the list you created is list=[0,1,2,3,4]..... print(Nums[4])>>>>>>>in list indexing Nums[4] is 4 that is because list start from 0....always remember... list=[0,1,2,3,4]>>>>>0 is 0,1 is 1, 3 is 3,4is4....even if the list is list=[3,5,6,5,4]>>>>3is0,5is1,7is2,6iz3 and 4 is 4....that's why your code printed '4'..….……HOPE THIS WAS HELPFUL
22nd Dec 2016, 2:50 AM
Seth
Seth - avatar
+ 2
range starts at 0 and goes the defined number of steps. So range (5) would return 0, 1, 2, 3, 4
22nd Dec 2016, 2:27 AM
James Durand
James Durand - avatar
0
Ok, I think I finally got it because I experimented on a list in the next question. There was a list like [3,4,5,6,7]. I asked for list[2] and got the printed response 5. That response seemed to clarify that the number 2 (as in list[2]) does not represent the 2nd position like many of us would have thought, but rather represents the position LABELED list[2]: That is remembering that the Labels for the positions in the list begin with list[0], then list[1], then for our sought after list[2]. So the element labeled list[2] represents the element in the 3rd position of the list, not the 2nd. -Hope I'm right and I hope that helps :)
20th Jan 2017, 2:34 AM
hank
0
Thank you for everyone!
20th Jan 2017, 1:52 PM
Kenzhebekov Nurtileu
Kenzhebekov Nurtileu - avatar