我有一个简单的代码来迭代范围内的所有元素
for i in range(5,10):
print(i)
#output
5
6
7
8
9
现在,是否可以按降序从 10 到 5 迭代相同的元素?通过将上述代码中的范围从 10 更改为 5 将不起作用
for i in range(10,5):
print(i)
#output not printed and no error displayed
你可以做类似的事情
for i in range(10,0,-1):
print(i)
这里-1
的意思是我们正在采取步骤-1
而不是默认的 1。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句