Python is now the most popular programming language because it is free and open-source, has a large standard library, and can be readily coupled with several programming languages. Among other elements, there are three types of loops in Python:
This article entails basic looping statements, i.e., while loop in Python and other statements and expressions used with while loop.
Python’s while loop statement is used to execute statements repeatedly (s). As the number of times the while syntax in Python runs is not known ahead of time, the while loop in Python is referred to as an Infinite Iteration statement. It will continue to execute repeatedly until the condition is true, and it will stop only if it turns false.
Assume you are instructed to print your favorite fruit name 50 times in Python. The most ignorant way would be to execute the statement print(“FruitName”) 50 times. But that’s a needless effort with 50 lines of code.
You will get the best outcomes if you utilize the while loop in Python. This is how it executes: Create a while loop, add the print(“FruitName”) within the while loop, add a condition that passes after 100 executions, and you’re done.
Unlike the droning approach, which requires 100 statements, this code is just 5-6 statements long. In simple words, the while syntax in Python simplifies our lives.
Here is a description of the elements in while syntax in Python:
Image Title: While loop flowchart in Python
Image Content: While loop, false, conditions true?, true, body of while loop execute statements, exit while loop
Image source: https://www.trytoprogram.com/images/python_while_loop.jpg
Example of while loop in Python
Program to print a favourite fruit seven times using the while loop in Python
add = 0;
while add < 7:
print(“My favourite fruit is mango”)
count = 1
Output:
My favourite fruit is mango
My favourite fruit is mango
My favourite fruit is mango
My favourite fruit is mango
My favourite fruit is mango
My favourite fruit is mango
My favourite fruit is mango
Explanation:
Python also allows else statements in combination with while statements and the else statement is not mandatory. The following cases illustrate how while else works:
while expression:
statement(s)
else:
Statement(s)
The body of ‘else’ can be one statement or a block of statements with consistent indentation. After the while loop condition returns false and the loop is ended, the else statement is run just once.
Example:
add = 0;
while add < 8:
print(“My favourite fruit is mango”)
add = 1
else:
print(“Favourite fruit is printed eight times.”)
Output:
My favourite fruit is mango
My favourite fruit is mango
My favourite fruit is mango
My favourite fruit is mango
My favourite fruit is mango
My favourite fruit is mango
My favourite fruit is mango
My favourite fruit is mango
Favourite fruit is printed eight times.
Explanation:
If the while loop’s body has only one statement, it can be inserted on the same line as the while header statement. Python supports this variant. The while syntax in Python below demonstrates:
while expression: statement
This is only permitted for a single statement. If there are many statements, they must be written within the body of the while loop and evenly nested.
Example:
add = 0
while add< 6: print(“I’m Alexa”)
Output:
The String “I’m Alexa” is printed indefinitely on the terminal.
Explanation:
The indefinite loop in Python is a loop that never ends since the expression (condition) stated in the while header will never return False. The instance in the next section is an example of an endless while loop.
In programming, there are several applications for the Infinite while loop. A basic example is a system/software that runs continuously, such as a server program. As a result, the client software can connect to the server program at any moment as long as the server program is functioning.
Let us look at an example indefinite loop Python program
add = 0
while add != 1:
name = str(input(“Your favorite color is? “))
print(“My favorite colour is” name)
Output:
Your favorite colour is? Black
My favorite colour is Black
Your favorite colour is? Blue
My favorite colour is Blue
Your favorite colour is? Grey
My favorite colour is Grey
Your favorite colour is? Black (recent call last):
File “main.py”, line 3, in <module>
name = str(input(“Your favourite color is? “))
KeyboardInterrupt
Explanation:
Loop control statements in Python control the sequence of execution of a while loop. They impede the usual execution of a loop. Python supports the following loop control statements:
Continue Statement:
Continue statement in the while loop body returns the flow of control to the expression in the header, bypassing the operation of other statements inside the body.
Example:
addition = 0
while addition < 10:
addition = 1;
if(addition == 5): continue
print(“The total is ” str(addition))
Output:
The total is 1,
The total is 2
The total is 3
The total is 4
The total is 5
The total is 6
The total is 7
The total is 8
The total is 9
The total is 10
Explanation:
Break Statement:
Break statement in the body of a while loop will terminate the continues and get the control out of the loop. It is like an abrupt force stop of a while loop. If it is a while..else statement, the else part is also terminated when a break statement is run. Control is given to the statement after the while..else statements.
Example:
addition = 0
while addition < 10:
addition = 1;
if(addition == 5): break
print(“The addition is ” str(addition))
Output:
The addition is 1
The addition is 2
The addition is 3
The addition is 4
Explanation:
Let us know the difference between for and while loop in Python:
Basis | For loop | While loop |
---|---|---|
Declaration | for value in range:
<statement> |
while condition:
<statement> |
Use loop | The loop is used when we know the number of iterations. | The loop is used when we don’t know the number of are required. |
Condition | body of the for loop will be executed indefinitely if there is no condition. | if the condition is missing, then it gives an error. |
Iteration | The statement is written at the top. | Statements can be written anywhere in the loop body. |
Operational process | control goes to the condition and checks the items from the sequence. It will terminate the statement and start again and check from the range it will choose the next value and run the statement and it will terminate until the range becomes zero. | Control will check inside the loop when while condition is true, if the condition is true, it will terminate the body and again check the condition. |
Initialization | If the condition is not true, control will not enter the loop. | if the condition is true, the loop is iterated. |
Examples: | alphabet = [‘e’,’f’,’g’]
for y in alphabet: print(y) |
a =4
while a < 8: print(a) a = 1 |
Conclusion
One of the many stages in mastering this exciting and powerful programming language is understanding the while loop in Python. Understanding Python opens the door for new opportunities in your professional career as big data and business analytics are widely welcomed across all domains.