0

I have a simple code to iterate over all the elements within the range

for i in range(5,10):
    print(i)
#output
5
6
7
8
9

Now, would it be possible to iterate the same elements from 10 to 5 in the decreasing order ? By changing the range in the above code from 10 to 5 won't work

 for i in range(10,5):
        print(i)
    #output not printed and no error displayed
vashista
  • 105
  • 7

2 Answers2

0

You can use reversed builtin method

for i in reversed(range(5, 10)):
    print(i)

An other option is to set step in range loop like range(10, 5, -1)

ahmed
  • 5,430
  • 1
  • 20
  • 36
0

You can do something like

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

The -1 here is saying that we are taking steps of -1 instead of the 1 that is default.

annes
  • 123
  • 1
  • 6