Python 递减循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4767401/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 17:25:36  来源:igfitidea点击:

Decrementing for loops

pythonfor-loopdecrement

提问by pandoragami

I want to have a for loop like so:

我想要一个像这样的 for 循环:

for counter in range(10,0):
       print counter,

and the output should be 10 9 8 7 6 5 4 3 2 1

并且输出应该是 10 9 8 7 6 5 4 3 2 1

采纳答案by user225312

a = " ".join(str(i) for i in range(10, 0, -1))
print (a)

回答by AndiDog

Check out the rangedocumentation, you have to define a negative step:

查看range文档,您必须定义一个否定步骤:

>>> range(10, 0, -1)
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

回答by Navi

You need to give the range a -1 step

你需要给范围一个 -1 步

 for i in range(10,0,-1):
    print i

回答by Isoon

range step should be -1

范围步长应为 -1

   for k in range(10,0,-1):
      print k

回答by PrithviJC

for i in range(10,0,-1):
    print i,

The range() function will include the first value and exclude the second.

range() 函数将包含第一个值并排除第二个值。