Python while loop
While loop is one which repeats a group of statements until the condition is made False, if not made False then the loop will never end. The while loop in python is same as the while loop in other programming languages.
While working with the while loop we need to follow the following 3 steps.
- Initialization of counter
- Condition checking
- Updating counter
Example:- Increment counter until counter < 5 by following above 3 steps.
Program
Input
while counter < 5:
print(counter)
counter += 1
Infinite while loop in Python
These kind of loops are mainly used in games.
There are 2 ways, by which we can create infinite while loop in python.
- Specifying the condition which remains True forever.
- Not updating the counter.
True condition in while loop
Example:- Instead of incrementing the counter, we will decrement it. which will goes till -infinity. And condition remains true forever.
Program
Input
while counter < 5:
print(counter)
counter -= 1
At the place of condition we can put True keyword, which will also work.
Program
Input
Not updating the counter in while loop
If you don't update the counter and won't make the condition false, then the loop will continue forever.
Program
Input
while counter < 5:
print(counter)
The break statement in while loop
The break statement will terminate / stop the while loop.
At a particular condition when we want to end the loop, we make use of the break keyword to terminate the loop.
Example:- terminate the loop when i = 6.
Program
Input
The continue statement in while loop
At some iteration when we want to skip the statements of a loop, we make use of the continue keyword.
When continue statement is triggered all the statements below that, in a loop will be skipped, and the next iteration continues.
Example:- In this while loop when x is 'even', the print statement will be skipped, and continues with the next loop.
Program
Input
Can we use else clause with while loop
Yes, it's possible to combine 'while' loop and 'else' clause.
When while loop fails, 'else' statement is executed.
Example:- here the while loop fails to find the character 's'.
Program
Input
It won't works with break. If the 'while' loop is terminated by the break statement, then the 'else' statement will not be executed.
Example:- In this the 'while' loop is terminated by the break statement, when number 5 is detected, and the 'else' statement is not executed.
Program
Input
It works with continue statement. The 'else' statement is executed after the completion of 'while' loop.
Example:- when x is even then the Iteration is continued, and after the loop is completed the 'else' statement is executed.
Program
Input
3
Loop through a list using while loop
Here is an example, which will loop through a list.
Program
Input
counter = 0
while counter < len(a):
print(a[counter])
counter += 1
3
4
5
6
7
Loop through a tuple using while loop
Looping through a tuple is same as, looping through a list. You can follow the above code for looping through a tuple using while loop.
Loop through a set using while loop
As set can't be accessed using index, so we have to make use of iter() method. Which will return an iterator object, by which we can access the values using index.
Here is an example.
Program
Input
counter = 0
while counter < len(a):
print(list(iter(a))[counter])
counter += 1
Loop through a string using while loop
Here is an example for you, which will loop through a string.
Program
Input
Loop through a dictionary using while loop
Fetching values while iterating through a dictionary, by making using the values() method.
Program
Input
Fetching keys while iterating through a dictionary
First fetching the keys from a dictionary using the keys() method. Converting all the fetched keys into list so that we can iterate over it using index. If we don't convert it into a list we will get an error.
Program
Input
With the following program we can get the values from the fetched keys.
Program
Input
Iterate through a dictionary using items() method
The items() method will return the collection of tuple 'key: value' pairs in a list. When we iterate through it, one 'key: value' pair will be fetched with each iteration, and to fetch the 'key' specify the index '0' and index '1' to get the value.
Program
Input
Conclusion
We have learned the use of break and continue statements with the 'while' loop.
And learned how to use else statement with the 'while' loop.
Also learned to loop through a list, tuple, set, string and dictionary.
Post a Comment
If you have any query, feel free to ask.