Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to arrange an array in decreasing order

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading