Python | Program to print numbers from N to 1 (use range() with reverse order)

Here, we will figure out how to print numbers backwards request for example the most effective method to utilize extend() technique in turn around request/diminishing advances.

Given the estimation of N and we need to print numbers from N to 1 in Python.

This strategy is utilized to emphasize a range of esteems.

Essentially, we use range(start, stop)

How about we comprehend by a model, on the off chance that we need to emphasize any circle till a to b, at that point go articulation will be a range(a b+1).

Emphasize backward request

To emphasize go backward request, we utilize 3 parameters

Start – start esteem

Stop – end esteem

Step – Increment/Decrement to the worth

Models:

1) To print numbers from B to A

for i in range(B, A-1, -1)
	print i

2) To print numbers from B to A by getting away one number between

for i in range(B, A-1, -2)
	print i

Program to print numbers from N to 1 in Python

# Python program to print numbers
# from n to 1

# input the value of n
n = int(input("Enter the value of n: "))

# check the input value
if (n<=1):
	print "n should be greater than 1"
	exit()
	
# print the value of n
print "value of n: ",n

# print the numbers from n to 1
# message
print "numbers from {0} to {1} are: ".format(n,1)

# loop to print numbers 
for i in range(n,0,-1):
	print i

Output

 Enter the value of n: 10
    value of n:  10
    numbers from 10 to 1 are: 
    10
    9
    8
    7
    6
    5
    4
    3
    2
    1

Output 2

 Enter the value of n: 1
    n should be greater than 1

Leave a Comment