[python] 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 either. How can I implement that?

This question is related to python loops for-loop

The answer is


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

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

As for

for n in reversed(range(0,6))

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


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

Late to the party, but for anyone tasked with creating their own or wants to see how this would work, here's the function with an added bonus of rearranging the start-stop values based on the desired increment:

def RANGE(start, stop=None, increment=1):
    if stop is None:
        stop = start
        start = 1

    value_list = sorted([start, stop])

    if increment == 0:
        print('Error! Please enter nonzero increment value!')
    else:
        value_list = sorted([start, stop])
        if increment < 0:
            start = value_list[1]
            stop = value_list[0]
            while start >= stop:
                worker = start
                start += increment
                yield worker
        else:
            start = value_list[0]
            stop = value_list[1]
            while start < stop:
                worker = start
                start += increment
                yield worker

Negative increment:

for i in RANGE(1, 10, -1):
    print(i)

Or, with start-stop reversed:

for i in RANGE(10, 1, -1):
    print(i)

Output:

10
9
8
7
6
5
4
3
2
1

Regular increment:

for i in RANGE(1, 10):
    print(i)

Output:

1
2
3
4
5
6
7
8
9

Zero increment:

for i in RANGE(1, 10, 0):
    print(i)

Output:

'Error! Please enter nonzero increment value!'

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

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

gives:

4
3
2
1
0

0 is conditional value when this condition is true, loop will keep executing.10 is the initial value. 1 is the modifier where may be simple decrement.

for number in reversed(range(0,10,1)):
print number;

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

For python3 where -1 indicate the value that to be decremented in each step for n in range(6,0,-1): print(n)


Examples related to python

programming a servo thru a barometer Is there a way to view two blocks of code from the same file simultaneously in Sublime Text? python variable NameError Why my regexp for hyphenated words doesn't work? Comparing a variable with a string python not working when redirecting from bash script is it possible to add colors to python output? Get Public URL for File - Google Cloud Storage - App Engine (Python) Real time face detection OpenCV, Python xlrd.biffh.XLRDError: Excel xlsx file; not supported Could not load dynamic library 'cudart64_101.dll' on tensorflow CPU-only installation

Examples related to loops

How to increment a letter N times per iteration and store in an array? Angular 2 Cannot find control with unspecified name attribute on formArrays What is the difference between i = i + 1 and i += 1 in a 'for' loop? Prime numbers between 1 to 100 in C Programming Language Python Loop: List Index Out of Range JavaScript: Difference between .forEach() and .map() Why does using from __future__ import print_function breaks Python2-style print? Creating an array from a text file in Bash Iterate through dictionary values? C# Wait until condition is true

Examples related to for-loop

List append() in for loop Prime numbers between 1 to 100 in C Programming Language Get current index from foreach loop how to loop through each row of dataFrame in pyspark TypeScript for ... of with index / key? Is there a way in Pandas to use previous row value in dataframe.apply when previous value is also calculated in the apply? Python for and if on one line R for loop skip to next iteration ifelse How to append rows in a pandas dataframe in a for loop? What is the difference between ( for... in ) and ( for... of ) statements?