[Solved] What is the use of -1 in Python for loop [duplicate]


It will decrement the value by 1 for each iteration

This value is called as step which tells the for loop to how to get the next value for the iteration. It can be both negative and positive, but not zero.

the positive step means it will increase the iteration value from the first argument to the second argument

example if step is 2 then 0,2,4,6,8,10

for i in range(0,10,2):
print(i) #0,2,4,8

The negative step means it will decrease the iteration value from the first argument to the second argument

For example if it is -2 then
10,8,6,4,2

for i in range(10,0,-2):
 print(i) #10,8,6,4,2

solved What is the use of -1 in Python for loop [duplicate]