Loop backwards using indices in Python?

Joan Venge picture Joan Venge · May 15, 2009 · Viewed 360.7k times · Source

I am trying to loop from 100 to 0. How do I do this in Python?

for i in range (100,0) doesn't work.

Answer

0x6adb015 picture 0x6adb015 · May 15, 2009

Try range(100,-1,-1), the 3rd argument being the increment to use (documented here).

("range" options, start, stop, step are documented here)