What happens if the step parameter of the range() function is very large?


#1

Question

In the context of this code challenge, what happens if the step parameter value of the range() function is very large?

Answer

Let us first recall the general syntax of the function, which is

range(start, stop, step)

The range() function returns a range of values from the start value, up to but excluding the stop value, where each value is essentially,
start + 1*step, start + 2*step, start + 3*step, ...

If we chose a very large step value, then there would not be any difference in how this function runs. It will only include values that are less than the stop value, so if a large step value would mean fewer elements in the range.

For example,

range(1, 10, 11)

For this range, we can see that the second value is 1 + 11 = 12, which is already greater than the stop value of 10. The function will simply exclude that value and then terminate. So, we would just get the following result, with just the start value.

result = list(range(1, 10, 11))
print(result) # [1]