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.


Discover more from Python Mania

Subscribe to get the latest posts sent to your email.

5 1 vote
Article Rating
Subscribe
Notify of
0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

Related Articles:

Recent Articles:

0
Would love your thoughts, please comment.x
()
x

Discover more from Python Mania

Subscribe now to keep reading and get access to the full archive.

Continue reading