Python While Loop
Until a specified criterion is true, a block of statements will be continuously executed in a Python while loop. And the line in the program that follows the loop is run when the condition changes to false.
Syntax of Python While
while expression: statement(s)
In Python, all the statements indented by the same number of character spaces after a programming construct are considered to be part of a single block of code. Python uses indentation as its method of grouping statements.
Python3
# prints Hello Geek 3 Times count = 0 while (count < 3 ): count = count + 1 print ( "Hello Geek" ) |
Output:
Hello Geek Hello Geek Hello Geek
See this for an example where a while loop is used for iterators. As mentioned in the article, it is not recommended to use a while loop for iterators in python.
Loops and Control Statements (continue, break and pass) in Python
Python programming language provides the following types of loops to handle looping requirements.