Range step


#1

can you guys explain what step means in passing a range into a function? because it explains that each item increases by step, so in this:
range(6) # => [0, 1, 2, 3, 4, 5]
range(1, 6) # => [1, 2, 3, 4, 5]
range(1, 6, 3) # => [1, 4]
shouldn’t it be range(1, 6, 3) #=> [4, 5, 6, 7, 8]?
because without the step it’s [1, 2, 3, 4, 5], and if step increases it by (in this case) 3 then it should be [4, 5, 6, 7, 8], what did i misunderstand?


#2

Hi @codewhiz78307,

Whether or not a step is given, the sequence begins with the specified start value. If a step value is given, each succeeding value in the sequence is generated by adding step to the previous value. The extent of the sequence is limited by the stop value.


#3

yeah, that’s what i thought, but that’s not what the example shows, because in the example it should be #=> [4, 5, 6, 7, 8], but it’s [1, 4] is this a mistake in the lesson, or am i just confused?


#4

range(1, 6, 3) results in [1, 4].

Since the start value is 1, it begins with 1. Then, since the step is 3, each value is 3 more than the previous value. That gives us 4 for the second value. If there were a value after that, it would be 7, except that the sequence is limited by the stop value of 6.


#5

ooooh, thanks i thought the “6” was referring to the number before you added the step


#6