How Can We Help?
This tutorial will explain how to use Python while loop, which is one type of Python loop statement with basic syntax by example for better understanding.
What is the while loop in Python? A while loop in Python repeats a part of code while a given condition is True, or the loop repeats until its condition becomes false. In the while loop, the condition evaluates first. Only if the condition is evaluated with TRUE, the program executes the code block inside the loop until the condition evaluates with false to exit the loop.
Python While Loop Syntax
while <condition>:
Body of while loop
- Conter_value: is a part of the body of the while loop. It is used to increase or decrease condition variable value after every iteration like a+=1, a-=1, etc.
When the program control reaches the while loop, the condition is checked. If the condition is true, the code block under it is executed. After one iteration, the condition is rechecked. This process continues until the condition becomes false. Then, the first statement, if any, after the loop is executed. The while loop body is determined through indentation, and all statements under the loop are equally indented.
Python While Loop Example
a=2
while(a>0):
print(a)
a-=1
print('While loop stop')
Output
2
1
While loop stop
In the above example, ‘a>0′ is a loop condition ,’print(a)’ is code of loop body and counter_value ‘a-=1’ means a=a-1 which is used to decrease the value of ‘a’ after every iteration. For the first iteration, ‘a=2’ is greater than ‘0’ and satisfies the condition, so the output shows as ‘2’ by executing the print statement.
For the second iteration, ‘a=1’ is greater than ‘0’ and satisfies the condition, so the output shows as ‘1’ by executing the print statement. For the third iteration, ‘a=0’ is not greater than ‘0’ and does not satisfy the condition, so the loop is stopped, and output returns the result of ‘print(‘While loop stop’)’, which is a first code line after while loop.
Python While Loop Single Statement
Using a physical line, we can make a single line while statement.
Example (1)
a=2
while(a>0):print(a); a-=1
Output
2
1
In the above example, two statements are available in the while’s body, and they are separated from each other by using semicolons.
Python While Loop with Multiple Conditions
We can use a logical operator (AND & OR) to have multiple conditions with the while loop.
Example (2)
var1 = 2
var2 = 7
counter = 0
while counter < var1 and counter < var2:
print(f"counter: {counter}, var1: {var1}, var2: {var2}")
counter += 1
Output
counter: 0, var1: 2, var2: 7
counter: 1, var1: 2, var2: 7
In the above example, we have two conditions using the logical operator “AND“. The first condition will be evaluated with TRUE and the second condition until the while loop reaches the third iteration, which the first condition will evaluate with false. However, the second condition is evaluated with TRUE, but because we are using “AND“, both conditions should be evaluated with TRUE to enter the loop.
Python Infinite Loop
A loop becomes an infinite loop if a while loop condition never becomes False and results in a never-ending loop. To stop the infinite loop, we can use Ctrl+C, which gives a KeyboardInterrupt error.
The main cause of an infinite loop is that we forget to provide increment or decrement counter detail in the while loop or write a flawed logic by which the condition never becomes false.
It is mostly used in client/server programming, where the server needs to run continuously so that client programs can communicate with it as and when required.
Example (3)
a=2
while(a>0):
print(a)
Output
2
2
2
2
2
2
2
2
Traceback (most recent call last):
File “C:\Users\Saurav\Desktop\Example1.py”, line 3, in
print(a)
File “C:\Python3\Python35\lib\idlelib\PyShell.py”, line 1344, in write
return self.shell.write(s, self.tags)
KeyboardInterrupt
In the above example, ‘a=2’ is greater than ‘0’ and satisfies the condition always because any counter value is not given in the loop. Hence, the output always returns the value as ‘2’ and makes an infinite loop that can be stopped by pressing ‘Ctrl+C’, which gives the ‘KeyboardInterrupt’ error in the output.
Conclusion
In this tutorial, you have learned how to use while loop in Python, you also learned how to use single line statement with while loop and we have explained how to use logical conditions like and & or to use multiple conditions in while loop.
Hopefully, it was clear and concise.
If you have an inquiry or doubt don’t hesitate to leave them in the comment section. we are waiting for your feedback.
Related Articles
Python Loop Statements – Comprehensive Guide
Python Tutorials List:
- Python tutorials list : access all python tutorials.