To print the first 10 natural numbers using the while loop can be done by repeating a block of code until a specific condition is met. While the loop does iteration until a specified condition is true.
Print first 10 natural numbers using while loop in Python
A simple example code runs the loop until “i” is greater or equal to 10. Increase the “i” value on every iteration.
i = 1 while i <= 10: print(i) i += 1
Use end-in-print to print in single-line output
# Initialize a variable to store the current number number = 1 # Use a while loop to print the first 10 natural numbers while number <= 10: print(number) number += 1
Output: 1 2 3 4 5 6 7 8 9 10
This code sets an initial value of
number to 1 and then enters a while loop. Inside the loop, it checks if
number is less than or equal to 10. If that condition is true, it prints the value of
number and then increments it by 1 using
number += 1. The loop continues until
number becomes greater than 10, at which point it exits.
Comment if you have any doubts or suggestions on this Python print with the while loop topic.
Note: IDE: PyCharm 2021.3.3 (Community Edition)
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.