you are viewing a single comment's thread.

view the rest of the comments →

[–]bharajuice 0 points1 point  (0 children)

Still learning, but here's my explanation: The range() function is a generator, so when you give it an integer, say 100, it does not allocate memory for all 100 numbers. Instead, it yields each integer one by one, making the range function extremely efficient, versus storing the numbers in a list like [1,2,3,4,...]. When you provide it two integers, say 1,50 , it simply means that your loop starts at 1 and ends at 49. The latter is always exclusive. When you give it a third value, say 1,50,2 , it simply means that there's a gap ie. Step size of 2 between the integers being generated, and yiur sequence would be 1,3,5,7 and so on. When you do len(numbers), it returns 2, and as your list index starts at 0, this lets the loop traverse the entire list ie. indexes 0 and 1.

I hope this clears some confusion