Python While Loop
While
loop is used when we want to perform iterations. It can repeat the statement or group of statements until the condition is true. As soon as the condition becomes false, it will stop iterating.
In the while
loop, it is mandatory to declare from where to start and the condition at which we would like to stop.
Below is the example of a while loop in which we are starting at a = 0,
the condition to terminate the while loop is a <= 0
. Note: In the while block, the variable 'a'
is getting incremented by 1, which is a must, otherwise the while
loop will get into an infinite loop.
a = 0
while a <= 5:
print(a)
a = a + 1
while
with string
While
loop can also be used to traverse the sequences, for example, string, list, tuple, etc. To achieve this, we can take help from range
function and string index positions. Let’s take a look at the example below:
a = 0
s = "Logical Python"
while a < len(s):
print(s[a])
a = a + 1
while
with else
This while else
is used when we want to do some task after the while loop is done with its iterations. The else block will execute only if the while
loop is successfully able to complete all the iterations without any error, exception, break statement, etc. If while
cannot complete all iterations, else
block will not get executed.
Here, else block saying, “Done!! We are Good” got successfully executed.
a = 1
while a <= 5:
print(a)
a = a+1
else:
print('Done!! We are Good.')
Here, else block is not executed because we used the break statement when a = 3.
a = 1
while a <= 5:
print(a)
if a == 3:
break
a = a+1
else:
print('Thanks for visiting')
Single Statement While
We can write the content of a while loop in a single statement. Each statement inside a while loop block has to be separated using the semicolon(;)
a = 1
while a <= 5: print(a); a = a+1
Infinite While loop
It is very important to update the condition variable inside the loop so that the loop can stop at some point. Otherwise, the loop will run infinite times until the memory is full. In this case, we have to manually stop/terminate the session.
In the below example, we have manually terminated the loop.
Infinite while loop. Removed iterations for sake of presentation.
i = 1
while i <= 5:
print(i)
Finite while loop
i = 1
while i <= 5:
print(i)
i = i + 1
Python For loop vs While loop
Here are a few differences between the python for loop and while loop.
Parameter | For Loop | While Loop |
---|---|---|
Keyword | For Keyword is used. | While Keyword is used. |
When to Use | The number of iterations already known. | The number of iterations is not known |
Iteration | Every element is fetched through the iterator/generator. Example, range() | Every element is explicitly incremented or decremented by the user |
Initialization | Only once at the start of the loop. | Repeat at every iteration. |
Generator Support | For loop supports generators in Python. | While loop does not support Generators directly. |
Efficiency | More efficient when iterating over sequences due to predetermined iterations. | Efficient in situations only where the stop condition needs to be evaluated. |
Loop Nature | Used to iterate over a fixed sequence of items. | Used when we don’t know the number of iterations. |
Speed | For loop is faster than a while loop. | While loop is slower as compared to for loop. |