Decrementing for loops

Question:

I want to have a for loop like so:

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

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

Asked By: pandoragami

||

Answers:

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

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

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

You need to give the range a -1 step

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

range step should be -1

   for k in range(10,0,-1):
      print k
Answered By: Aysun Itai
for i in range(10,0,-1):
    print i,

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

Answered By: PrithviJC
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.