In this Python post, we’ll learn what Python while loop is and how to properly use it.
We’ll explain it practically using multiple examples so you can have a better idea of how to use while loop in Python.
So without wasting anymore of your valuable time, let’s just get right into its implementation.
What is Python While Loop?
As the name suggests, it is used for looping or you can say its used for repetition. While loop has a test expression which is checked and if it returns true then its body is executed. If its false then the loop stop/break and the code after while loop executes.
Let’s now understand Python while loop usage with the help of practical examples. But first, we have to understand its syntax.
Syntax of While Loop
while condition:
// body of while loop
First comes the while keyword, then a condition is checked and if it is satisfied then the while loop executes and stop/come out of loop if it return false.
Implementing Python While Loop (Multiple Examples)
See the below detailed examples on how to properly use while loop in Python.
Example 1: While Loop on Integer
val=0; while val!=5: print('5 not found yet') val+=1 print('5 found successfully')
Output
5 not found yet 5 not found yet 5 not found yet 5 not found yet 5 not found yet 5 found successfully
We have initialized an integer with a value 0 and have specified a condition that if the value is not equal to 5 then run the while loop. The value will also get incremented by 1, everytime the while loop executes.
You can see that the loop execute for 5 times(0,1,2,3,4) and when its value reaches 5 then the condition will again check and it will return false as it was specified that only execute the while loop body when the value is not equal to 5. So it will stop the while loop and as a result, the statement outside while loop gets executed.
Example 2: While Loop on List
Let’s now use the same process to apply Python while loop on list as well. See below code:
listOfColors=['white','green','blue','purple','grey'] index=0; // use to get the index of each item of list while listOfColors[index]!='purple': // each item will be checked using the index print(listOfColors[index]) index+=1 // increase the index by 1 so 0,1,2,3, etc print('Purple color is found') // executes when the while loop break/stop
Output
white green blue Purple color is found
You can see that the while loop continues to execute but when the condition returns false, then it stops and the statement outside this loop gets executed.
Example 3: While Loop with Else Statement
While loop also has an else statement which will execute if the while condition return false. It can be used to specify if you want to perform a specific action if the while loop return false. Using break statement in while loop will make the while loop stop executing and the control will come out of it. As a result, the else statement will not execute. See below code:
index=0; while index<5: print(index) index+=1 else: print('Else statement is executed')
Output
0 1 2 3 4 Else statement is executed
You can see that the loop was executing successfully but when the condition returns false then the else statement got executed.
So this is how you can easily use Python while loop in your own code as well.
Don’t hesitate to ask if you still have any questions related to the implementation of Python while loop. I’ll be happy to answer your questions.
Conclusion
To conclude, we have practically discussed how to use Python while loop with multiple examples for better understanding. I’ll be looking forward at your feedback on this post. Thank you for reading it.