Skip to content Skip to sidebar Skip to footer

Decreasing For Loops In Python Impossible?

I could be wrong (just let me know and I'll delete the question) but it seems python won't respond to for n in range(6,0): print n I tried using xrange and it didn't work eith

Solution 1:

for n inrange(6,0,-1):
    print n
# prints [6, 5, 4, 3, 2, 1]

Solution 2:

This is very late, but I just wanted to add that there is a more elegant way: using reversed

for i inreversed(range(10)):
    print i

gives:

4
3
2
1
0

Solution 3:

for n in range(6,0,-1)

This would give you 6,5,4,3,2,1

As for

for n inreversed(range(0,6))

would give you 5,4,3,2,1,0

Solution 4:

for n in range(6,0,-1):
    print n

Solution 5:

>>>range(6, 0, -1)
[6, 5, 4, 3, 2, 1]

Post a Comment for "Decreasing For Loops In Python Impossible?"