for i in range(1,4,-1):
print(i)
Here , "i" will be initialized as "1" which lies in [1, 4) , So it should print 1 , but it doesn't print anything. Why ?
for i in range(1,4,-1):
print(i)
Here , "i" will be initialized as "1" which lies in [1, 4) , So it should print 1 , but it doesn't print anything. Why ?
Here your step value is negative
For a positive step, the contents of a range r are determined by the formula r[i] = start + step*i where i >= 0 and r[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 are i >= 0 and r[i] > stop.
The first value would be 1 + (-1) * 0 = 1, which is less than your step value (4) and this fails the second constraint.
so if you go through the formula, its obvious nothing would get printed - Reference
In order to make it work you should have: range(4, 1, -1).
As said on the documentation :
For a positive step, the contents of a range r are determined by the formula r[i] = start + step*i where i >= 0 and r[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 are i >= 0 and r[i] > stop.
According to this part of the documentation:
For a negative step, the contents of the range are still determined by the formula r[i] = start + step*i, but the constraints are i >= 0 and r[i] > stop.
Your first value would be 1 + (-1) * 0 = 1, which is less than your stop value of 4.
So the second constraint applies, and this value isn't kept. Your range is empty.