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