In chapter 11 of Python Tutorial, we will learn about Loops (Part 2) – While Loop
If you are reading this tutorial on Mobile, please rotate your mobile horizontally for best reading experience. You can enable auto-rotate in your phone settings.
A while loop iterates over block of code “n” number of times until a boolean condition is satisfied.
Before executing block of code, while loop will first check the condition and if the condition is True, it will execute the code. This loop will continue until the condition is no longer True. Let’s look at an example:
x = 0
while x < 5:
print(x, "Condition is True")
x += 1 #incrementing the value of x by 1. Same as x = x + 1
In the above example, it will print out the x value and also, statement "Condition is True" until x value is no longer less than 5.
Let's use else statement in the above while loop example:
x = 0
while x < 5:
print(x, "Condition is True")
x += 1 #incrementing the value of x by 1. Same as x = x + 1
else:
print(x, "Condition is no longer True")
Till the condition is True, code block inside while loop is executed. Once the condition is no longer True then, the else statement is executed. In this way, we can use else statements in while and for loops.
"break" and "continue" statements:
Using "break" and "continue" statements in for or while loops.
To have more control on the flow of for or while loops, we can use "break" and "continue" statements.
"break" statement is used to stop the execution and exit the for or while loop prematurely.
"continue" statement is used to skip executing the current block of code and return the control to the beginning of for or while loop.
Let's look at below examples:
#break statement with while loop
count = 0
while True:
print(count)
count += 1
if count >= 5:
break
#continue statement with for loop
fruits = ["Apples", "Bananas", "Oranges", "Pineapples"]
for fruit in fruits:
if fruit == "Oranges":
continue
print(fruit)
In the 1st example, break statement stops the execution of the loop after variable "count" is equal to 5. In the 2nd example, continue statement skips the execution and returns to the beginning of the loop after variable "fruit" is pointing to "Oranges".
The main difference is break statement completely exits the loop, whereas continue statement does not completely exits out of the loop; rather, it pauses the current iteration at a given point and moves on to the following element of the iterable object.
Assignment:
Write a program to print the numbers from 1 to 15, however, I want you to exit the loop prematurely and print the numbers till 9 only.
#Print out 1,2,3,4,5,6,7,8,9
count = 0
#write your code here
#Print out 1,2,3,4,5,6,7,8,9
count = 0
while count < 15:
count += 1
if count == 10:
print("Exiting the loop prematurely")
break
print(count)
That brings to the end of chapter 11 of Python tutorial, see you in the next tutorial!!!
By the way, if you haven't installed Python and Pycharm IDE on your Windows laptop, you can visit the below links to learn on how to install and configure/install Python and Pycharm IDE on Windows OS. This might be helpful in understanding the Python language easier.
How to Install Python 3 and Pycharm IDE on Windows
How to configure Pycharm IDE and "Run" your first Python 3 program
For your reference, to download Pycharm IDE, visit below link:
https://www.jetbrains.com/pycharm/download/?section=windows
However, if you don't want to go through installation process, you can practice your learnings on this page itself by using integrated Python IDE above.
If you have any questions or comments about chapter 11 of Python tutorial, please let us know in the comments section below.