0
Range. Why is 4 the right answer?
When learning about Range Function in Python, this came up with the question "What is the result of this code?" nums = list(range(5)) print(nums[4]) So, as far as I know the list would be [0,1,2,3,4,5], but since it is asking to print nums[4] I believe the answer should be number 3, since number 3 is the fourth item on the list, but when I try it says number 4 is the right answer for this question. How is number 4 the right answer?
7 odpowiedzi
+ 1
Print the "nums" to see the lists' content.#
indexes starts at 0 not 1.
0
rodwynnejones when it says print (nums [4]) shouldnt it print the fourth item of the list [0,1,2,3,4] (i got the list wrong last time) instead of printing the number 4?
0
range() function does not include the last(stop) number in the result and we are starting from 0
0
using your example->[0, 1, 2, 3, 4, 5]
nums[0] -> 0
nums[1] -> 1
nums[2] ->2
...etc
if the list was [1, 2, 3, 4, 5, 6]
nums[0] -> 1
nums[1] -> 2
nums[2] -> 3
....etc.
0
See the range function.gives output as x-1 for input x and in the list the indexing starts from 0,1,2....
When asked for nums[4] it gives answer as 3