Python range() with negative strides

前端 未结 7 1118

Is there a way of using the range() function with stride -1?

E.g. using range(10, -10) instead of the square-bracketed values below?

<
7条回答
  •  孤街浪徒
    2020-12-09 15:16

    You may notice that the range function works only in ascending order without the third parameter. If you use without the third parameter in the range block, it will not work.

    for i in range(10,-10)
    

    The above loop will not work. For the above loop to work, you have to use the third parameter as negative number.

    for i in range(10,-10,-1) 
    

提交回复
热议问题