Python Print 1 to 10 with a while Loop
We will use Python's while loop to print the numbers from 1 to 10.while A loop keeps executing until the given condition is no longer satisfied.
Example
i
=
1
while
i
<=
10
:
(
i
)
i +
=
1
Code analysis:
i = 1: Initialize the variablei, setting its value to 1.while i <= 10:: This is awhileloop; as long as the value ofiis less than or equal to 10, the loop keeps executing.print(i): In each iteration, print the currentivalue.i += 1: After each iteration, increase the value ofiby 1.
Output result:
1
2
3
4
5
6
7
8
9
10
Python3 Examples