[Tutor] xrange

Alan Gauld alan.gauld at btinternet.com
Tue Mar 18 23:06:19 CET 2008

"elis aeris" <hunter92383 at gmail.com> wrote

> for x in xrange (20, 0):
>    print x
> this doesn't work because it goes from a big number to a small 
> number, which
> does nothing
> but what if I need the for loop to go from a big number to a small 
> number?

Add a 3rd step-value argument to range. (You don't need the xrange
on modern versions of Python BTW)

If you make the step negative it will count down:

>>> range(20,0,-4)
[20, 16, 12, 8, 4]

Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list