When we execute the code
for i in range(10,0,-1):
print(''' ''',i)
We get the result as 10 to 1(including 1) But we do know that stop value is always n-1. So shouldn't it come until -1. Or does n-1 depend on the step value? Like if positive step value comes then n-1 and when negative step value comes n 2.
CodePudding user response:
Stop value is not n-1
but it is n-step
, and your step = -1
So,
Stop value = n - step = 0 - (-1) = 1
CodePudding user response:
From the Python documentation
class range(start, stop[, step])
<snip>
For a positive step, the contents of a range r are determined by the formula
r[i] = start step*i
wherei >= 0
andr[i] < stop
.For a negative step, the contents of the range are still determined by the formula
r[i] = start step*i
, but the constraints arei >= 0
andr[i] > stop
.
So, in case of range(10,0,-1)
the return values all have to be bigger than 0
.