70

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

Felix Kling
  • 756,363
  • 169
  • 1,062
  • 1,111
pandoragami
  • 5,117
  • 14
  • 61
  • 110

5 Answers5

108
a = " ".join(str(i) for i in range(10, 0, -1))
print (a)
Eimantas
  • 48,240
  • 16
  • 132
  • 164
user225312
  • 118,119
  • 66
  • 167
  • 181
56

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]
AndiDog
  • 65,893
  • 20
  • 156
  • 201
17

You need to give the range a -1 step

 for i in range(10,0,-1):
    print i
Navi
  • 8,272
  • 4
  • 33
  • 32
11
for i in range(10,0,-1):
    print i,

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

PrithviJC
  • 393
  • 3
  • 9
1

range step should be -1

   for k in range(10,0,-1):
      print k
Aysun Itai
  • 21
  • 4
  • Could you please elaborate more your answer adding a little more description about the solution you provide? – abarisone Jun 30 '15 at 07:39