Python - While Loops
Python - While Loops
If it fails to turn false, the loop continues to run, and doesn't stop unless forcefully
stopped. Such a loop is called infinite loop, which is undesired in a computer program.
while expression:
statement(s)
In Python, all the statements indented by the same number of character spaces after a
programming construct are considered to be part of a single block of code. Python uses
indentation as its method of grouping statements.
https://www.tutorialspoint.com/python/python_while_loops.htm 1/6
10/23/24, 9:53 AM Python - While Loops
Example 1
The following example illustrates the working of while loop. Here, the iteration run till
value of count will become 5.
Open Compiler
count=0
while count<5:
count+=1
print ("Iteration no. {}".format(count))
Iteration no. 1
Iteration no. 2
Iteration no. 3
Iteration no. 4
Iteration no. 5
End of while loop
https://www.tutorialspoint.com/python/python_while_loops.htm 2/6
10/23/24, 9:53 AM Python - While Loops
Example 2
Here is another example of using the while loop. For each iteration, the program asks for
user input and keeps repeating till the user inputs a non-numeric string. The isnumeric()
function returns true if input is an integer, false otherwise.
var = '0'
while var.isnumeric() == True:
var = "test"
if var.isnumeric() == True:
print ("Your input", var)
print ("End of while loop")
enter a number..10
Your input 10
enter a number..100
Your input 100
enter a number..543
Your input 543
enter a number..qwer
End of while loop
An infinite loop might be useful in client/server programming where the server needs to
run continuously so that client programs can communicate with it as and when required.
Example
Let's take an example to understand how the infinite loop works in Python −
var = 1
while var == 1 : # This constructs an infinite loop
num = int(input("Enter a number :"))
print ("You entered: ", num)
print ("Good bye!")
https://www.tutorialspoint.com/python/python_while_loops.htm 3/6
10/23/24, 9:53 AM Python - While Loops
The above example goes in an infinite loop and you need to use CTRL+C to exit
the program.
Learn Python in-depth with real-world projects through our Python certification
course. Enroll and become a certified expert to boost your career.
https://www.tutorialspoint.com/python/python_while_loops.htm 4/6
10/23/24, 9:53 AM Python - While Loops
Example
The following example illustrates the combination of an else statement with a while
statement. Till the count is less than 5, the iteration count is printed. As it becomes 5, the
print statement in else block is executed, before the control is passed to the next
statement in the main program.
Open Compiler
count=0
while count<5:
count+=1
print ("Iteration no. {}".format(count))
else:
print ("While loop over. Now in else block")
print ("End of while loop")
Iteration no. 1
Iteration no. 2
Iteration no. 3
Iteration no. 4
Iteration no. 5
https://www.tutorialspoint.com/python/python_while_loops.htm 5/6
10/23/24, 9:53 AM Python - While Loops
Example
The following example shows how to use one-line while clause.
Open Compiler
flag = 0
while (flag): print ("Given flag is really true!")
print ("Good bye!")
When you run this code, it will display the following output −
Good bye!
Change the flag value to "1" and try the above program. If you do so, it goes into infinite
loop and you need to press CTRL+C keys to exit.
https://www.tutorialspoint.com/python/python_while_loops.htm 6/6