72

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
  • 795,719
  • 175
  • 1,089
  • 1,143
pandoragami
  • 5,387
  • 15
  • 68
  • 116

5 Answers5

110
a = " ".join(str(i) for i in range(10, 0, -1))
print (a)
Eimantas
  • 48,927
  • 17
  • 132
  • 168
user225312
  • 126,773
  • 69
  • 172
  • 181
57

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
  • 68,631
  • 21
  • 159
  • 205
18

You need to give the range a -1 step

 for i in range(10,0,-1):
    print i
Navi
  • 8,580
  • 4
  • 34
  • 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