Now we are going to Print numbers from 10 to 1 in reverse order in Python using a while loop:
Simple code:
num = 10
while num >= 1:
print(num)
num = num - 1
Output:
10
9
8
7
6
5
4
3
2
1
Code with comments:
#initialize the starting number to 10
num = 10
#continue looping until the number reaches 1
while num >= 1:
# print the current number
print(num)
# decrement the number by 1
num -= 1
Output:
10
9
8
7
6
5
4
3
2
1
In this code, we initialize the starting number to 10 and then use a while loop to continue printing numbers until we reach 1.
We print the number at each iteration of the loop and then decrement the number by 1. This results in printing the numbers in reverse order, from 10 to 1.
Discover more from Python Mania
Subscribe to get the latest posts sent to your email.