Introduction to Python 3 While Loop
Python 3 While Loop is a programming construct that is used to repeat a set of statements until a condition is met. The program code line after while loop is performed when the condition becomes false. Indefinite iteration is the category in which loop falls. Multiple numbers of times while loop is run and it was not set expressly in advance, which is known as indefinite iteration.
What is Python 3 While Loop?
- In the Python programming language, while loop statement is executing the statement of target repeatedly as long as the specific condition is true.
- In programming, loops are used to repeat specified blocks of code. In Python, while loop iterates through code block to test expression.
- At the time we don’t know how many times we’ll iterate ahead of time, we’ll utilize this loop.
- After using the while loop in python expression of the program is first checked. Only if the true value is evaluated by test expression is the body of the loop entered. The expression of the test is verified again after one cycle.
Python 3 While Loop Statement
Typically, the controlling expression, expr>, includes more than one or one variable is initialized before the loop starts and subsequently updated anywhere in the loop body. At the time while loop in python 3 met, the variable expr> is evaluated in a Boolean context first. The body is then re-executed if expr> is still true.
This continues until expr> is set to false, at which point the program moves on after the loop body. Python does a condition check first. If it’s False, the loop will end and control will be handed in the loop body. The loop body is run if the condition is true, if the condition is False then the condition is checked again. As long as the condition is true, this will continue. The loop ends while the condition was false, and control is handed to the following sentence.
Below syntax shows how while statement we are using in python code:
Expression of while Statement (s)
After a programming construct, statements are regarded to be part of a single block of code, which is referred to as a statement. The expr is then tested again, and if it is still true, the body is run once more, and so on until the expression is no longer true. A statement consistent indent may be used in this case. Python’s approach for grouping statements is called indentation. When a while loop is run, expr in a Boolean context. When the condition is false, the program control is passed to the next line in the loop. All statements after a programming construct are considered part of a single block of code in Python. Python’s approach for grouping statements is called indentation.
The below example shows python 3 while loop statement as follows:
py = 11 while py < 21: print(py) py += 1
In the above example, we can see that the number is printed from 11 to 20. We have used increment value as 1 so the value is incremented by 1. At the time using value as 2 the pointer will be incremented with 2 positions.
Below example shows that the pointer is incremented with 2 positions while using incrementer as 2.
py = 1 while py < 6: print(py) py += 2
Initially, py is set to 1. The expression n < 6, which is true, hence the loop body runs. Py is incremented by 2 and then written inside the loop body. Program execution returns to line 2 at the start of the loop, where the expression is re-evaluated. Because it is still true, the body executes once more, and the number 5 is printed. At the time given condition is True, a while loop executes a piece of code. It will continue to execute the required collection code until the condition no longer holds true. Before running, a while loop always checks the condition. The loop will execute the code within the loop’s body.
Python 3 While Loop Flowchart
In the below flowchart, we can see that the expression will handle by using conditions.
The while loop’s important feature here is that it may or may not ever run. The loop body is skipped and the first line after the while loop is executed. In the above figure, we can see that after starting execution of code it will go to see the statement expression and check the condition. If the expression condition is false interpreter will directly go outside of the loop. If the expression condition is true it will execute the statement within the while statement.
Examples of Python 3 While Loop
Given below are the examples mentioned:
Below is the example of python 3 while loop. Below example shows while loop with single string.
py = 0 while (py < 10): py = py + 1 print("Python 3 while loop")
In the above example, we can see “Python 3 while loop” statement will be executed 10 times because the while loop is executed 10 times in the condition which was we have used in code.
The below example shows a while loop with the list. We also use a list at the time using the while loop in python.
py = [15, 29, 38] while py: print(py.pop())
Below example shows while loop with continue statement is as follows.
py = 0 py1 = 'Python 3 while loop' while py < len(py1): if py1[py] == 'l' or py1[py] == 'n': py += 1 continue print('Letter is :', py1[py]) py += 1
The below example shows python 3 while loop using break statement are as follows.
py = 0 py1 = 'Python 3 while loop' while py < len(py1): if py1[py] == 'e' or py1[py] == '3': py += 1 break print('Letter is :', py1[py]) py += 1
Python’s approach for grouping statements is called indentation. When a while loop is run, expr in a Boolean context, and then the loop body is run if it is true. Python 3 while loop is a programming construct that is used to repeat a set of statements until a condition is met.
This is a guide to Python 3 While Loop. Here we discuss the introduction, python 3 while loop statement, flowchart, and examples. You may also have a look at the following articles to learn more –