Python — while loop
Another method for looping through a number of sequence is the while loop. The ‘while’ loop executes the set of statements as long as the condition is true. When the condition becomes false the program moves to the next line outside of the loop.
We generally use this loop when we don’t know the number of times to iterate beforehand.
Get GIS-ish’s stories in your inbox
Join Medium for free to get updates from this writer.
Now we are going to count up to 4 and print “ Good bye ! ”
count = 0while (count < 4): print('The count is:', count) count = count + 1print("Good bye!")The count is: 0
The count is: 1
The count is: 2
The count is: 3
Good bye!
- while + else
We can do this with the else statement. The else part is executed if the condition in the while loop evaluates to False.
count = 0while (count < 4): print('The count is:', count) count = count + 1else: print('Good bye!')The count is: 0
The count is: 1
The count is: 2
The count is: 3
Good bye!
On the fifth iteration, the condition in while becomes False. Hence, the else part is executed.
For more information, please have a look at the sites linked below.









