In this tutorial, we will cover the basics of Python while loop. This is a similar one to the Python for loop with one difference. You can use the while loop where you do not know the number of iterations beforehand. So until the condition specified to the loop is true, the loop will run.
Table of Contents
Syntax of the while loop in Python
As we’ve already discussed, the while loop takes a single condition. When the condition evaluates to true, the while loop will continue to run. As soon as the condition is no longer true, the loop stops.
So in this case, if you create a loop with the condition as “TRUE”, the loop will run infinitely until a break or continue statement is added.
While condition : #Start of the statements Statement . . . . . . . Statement #End of the Statements else : #this scope is optional #This statements will be executed if the condition #written to execute while loop is false
1. Simple example of the while loop
For example, the following code will give you some ideas about the while loop. In this example, we are printing numbers from 1 to 4 inside loop and 5 in outside of the loop
cnt=1 #this is the initial variable while cnt < 5 : #inside of while loop print (cnt,"This is inside of while loop") cnt+=1 else : #this statement will be printed if cnt is equals to 5 print (cnt, "This is outside of while loop")
To give you a real-world example, let’s try to create a loop where we want to print out individual characters of a word here. We’ve already performed this example with the for loop, but this one will work with the while loop.
word="anaconda" pos=0 #initial position is zero while pos < len(word) : print (word[pos]) #increment the position after printing the letter of that position pos+=1
The benefit of the while loops in Python is that you can ignore the number of iterations and break the condition as soon as a specific task is complete.
2. Python Nested while loop
Let’s start working with a nested while loop in this case. A nested while loop helps you work with the iterator variable while the loop continues to run. Take a look at the example below:
line=1 #this is the initial variable while line <= 5 : pos = 1 while pos < line: #This print will add space after printing the value print pos #increment the value of pos by one pos += 1 else: #This print will add newline after printing the value print pos #increment the value of line by one line += 1
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
3. An infinite while loop
As we mentioned earlier, the while loop in Python works on a single condition. And as long as the condition evaluates to true, the loop continues to run.
And that’s where a problem arises – The infinite while loop problem.
Take a look at the example below:
var = 100 while var == 100 : # an infinite loop data = input("Enter something:") print ("You entered : ", data) print ("Good Bye Friend!")
If you run the above program, it will never end and you will have to kill it using the Ctrl+C keyboard command.
>>> ================= RESTART: /Users/pankaj/Desktop/infinite.py ================= Enter something:10 You entered : 10 Enter something:20 You entered : 20 Enter something: Traceback (most recent call last): File "/Users/pankaj/Desktop/infinite.py", line 3, in <module> data = input("Enter something:") KeyboardInterrupt >>>
That’s all to the Python while loop. We continue to cover a lot more advanced Python tutorials if you’re interested. Read this article if you’re interested in getting started with Machine learning in Python.