Print Numbers from 1 to 10 Using While Loop in Python

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.

Was this helpful?
YesNo

Related Articles:

Recent Articles:

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x