In this tutorial, you will learn about Python while loop using an appropriate example. Python while is used, when you want to execute a block of code a number of times.
In previous we have seen details about Python if-else statement using appropriate syntax and examples. To learn Python if-else statement, click here.
Before going through this tutorial let’s see what exactly is a while loop in Python and why you need to use a while loop in Python?
What is Python while loop ?
Python While loop is a loop that is used to execute a set of statements as long as the condition is True.
In the while loop when the test expression evaluates True, then execute the body of the while loop, and if the test expression evaluates False then exit the loop.
Why should be using Python while loop ?
Sometimes we want to execute a block of code a specific number of times, Then while loop in Python is the best way at that time.
Syntax of while loop in Python.
while test expression: body of while
In the above while loop syntax first checks the condition and enters the body when the condition becomes True and if the condition is False then exit the loop.
While loop flowchart
Python while loop Example:
To understand the while loop in Python we take a suitable example that better understands you.
def counter(): i = 0 while i < 10: print("i is ",i) i = i + 1 counter()
i is 0 i is 1 i is 2 i is 3 i is 4 i is 5 i is 6 i is 7 i is 8 i is 9
In the above example create a function counter and inside the function define a variable i which initial value is 0. print the value of i as long as the condition is true when the condition will false loop will be terminated.
While loop with else statement
Python provides a facility to use else keyword with a while loop. Sometimes we need to print some messages just end after the while loop execution then we can use the else keyword.
let’s understand how we can use else keyword with the while loop.
def calculator(): i = 1 while i < 11: print("Condition true") i = i + 1 else: print("execution is finished") calculator()
Condition true Condition true Condition true Condition true Condition true Condition true Condition true Condition true Condition true Condition true execution is finished
In the above example, print Condition true as long as the condition will True and also print the execution is finished message after the end of while loop execution.
Python while loop with a break statement:
To stop the execution even if the condition is True in python while loop, use the break statement. break statement define with the break keyword
counter = 1 while counter < 10: print("counter") if counter == 5: break counter = counter + 1
counter counter counter counter counter
In the above example, the counter is initialized with value one and checking the condition, if the value of the counter will be less than 10 then the print counter and if the value counter will equal to five, Then loops will terminate.
Python while loop with continue statement:
To stop the current iteration and continue with the next iteration in the while loop, use the continue statement.
counter = 0 while counter < 10: counter = counter + 1 if counter == 5: continue print(counter)
1 2 3 4 6 7 8 9 10
So, In this article, you have learned what is Python while loop is and how you can use the while loop in Python to execute a block of code a specific number of times.
Sometimes we want to write a program that performs a task as long as the condition is True, Then while loop in Python loop fulfills that condition.
I think this will help you. if you like this post please, please share it with your friends who want to learn Python programming from scratch.
Reference:- Click Here