A Complete Guide to While loops in Python
|In python, we have generally two-loop commands
- for loop
- while loop
In this Python article, we will discuss the while loop with some examples.
1. What are while loops in Python?
while
loop executes a statement block again and again until the given condition is true or satisfied. But if the condition is encountered to be false, the loop will not be executed any further.
while
loop is a type of indefinite iteration, it means that how many times the loop will be executed is not specified explicitly before the loop begins.
If there are lines below the while
loop, they will be executed after the program has dealt with the full execution or condition check of the while loop.
Syntax: while condition/expression : statement
When we execute a while
loop, the condition is computed in boolean form. If TRUE, the statements inside the loop will be executed.
The condition is checked repeatedly and executed again and again until the condition has become FALSE.
Lets take an example to understand the working of while
loop
# An example for while loop check = 10 while(check > 0): check = check - 2 print(check, end=', ') print() # Illustration with list list = [5, 6, 7, 8, 9] while list: print(list.pop(), end=', ')
Output 8, 6, 4, 2, 0 5, 6, 7, 8, 9
2. Single line while loop
We can write the while
loop in a single line, for multiple statements in the block to make the loop body, we can use the semicolon(;
) to separate them.
Note: We can write only simple statements in a single line while loop. We cannot combine if condition with a single while loop.
check = 12 while(check > 5): check = check - 2; print(check, end=', ')
Output 10, 8, 6, 4
Let’s see how we can access else after while loop.
3. While loop with else clause
As we now know, when the condition of the while loop becomes FALSE, the loop stops execution. We can write else
clause which will be executed when the condition becomes FALSE.
Note: Else block will not work if we use ‘break
‘ in the loop or when an exception
arises
# while-else loop check = 0 while check < 10: check += 3 print(check, end=', ') else: # This block will be executed since there is no break print("\nRunning else block without break") check = 0 while check < 10: check += 2 print(check, end=', ') break else: # This block will not be executed since there is break print("Running else block")
Output 3, 6, 9, 12, Running else block without break 2
4. Break and Continue in While Loop
As till now, we saw the while
loop working on each iteration but we can also terminate the loop iteration with the help of some loop control statements – break and continue.
Break
statement in Python, which terminates the entire loop immediatelycontinue
statement is Python, which terminates a current iteration in the loop
Lets take an example to understand the Python break statement
# while-break statement count = 3 while count >= 0: if count == 1: print("Start") break print(count) count = count-1
Output 3 2 Start
Here, when count
will be 1, the break
statement will be executed and the loop will terminate.
Now let’s take an example to understand the Python continue statement
# while-continue statement count = 60 while count > 10: count -= 10 if count == 30: continue print(count)
Output 50 40 20
Here, when the count
will be 30, the continue will terminate the current iteration and it will not print 30
and it will move to the next iteration and print 20
To know more about the break
and continue
statements, we have a post on break and continue in python with a detailed explanation.
5. Nested While Loop
In python, we can use nested while
loop which means we can write a while
loop inside the body of another while loop.
Here’s an code example to understand how it works.
# nested while loop list1 = ['fruit', 'colour'] while len(list1): print(list1.pop(0)) list2 = ['orange'] while len(list2): print('--', list2.pop(0))
Output fruit --orange colour --orange
In addition to the above, we can also nest if/elif/else
statements inside the while
loop and vice versa. Let’s have a look at the syntax.
While loop inside if-else if condition_1: while condition1: statements statements elif condition_2: while condition2: statements statements
if-else blocks inside while loop while condition: if condition_1: statements elif condition_2: statements statements
6. Conclusion
In this article we learned about
- while loop
- while loop with else block
- Single line while loop and usage of break and continue
- Nested while loop
Helpful Links
Please follow the Python tutorial series or the menu in the sidebar for the complete tutorial series.
Also for examples in Python and practice please refer to Python Examples.
Complete code samples are present on Github project.
Recommended Books
An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding
Do not forget to share and Subscribe.
Happy coding!! ?