Python while loops




Syntax :

 while expression: statement (s ) 

Operators represent all operators indented the same number of character spaces after a code construct is considered part of a single block of code. Python uses indentation as a method for grouping statements. When a while loop is executed, expr is first evaluated to a Boolean context and, if true, the body of the loop is executed. Then expr is checked again, if it is still true, then the body is executed again and this continues until the expression becomes false.

Example:

# Python program for illustration
# loop

count = 0

while (count & lt; 3 ): 

count = count + 1

print ( "Hello Geek"

 

print ()

 
# checks if the list is still
# contains any element

a = [ 1 , 2 , 3 , 4 ]

while a:

print (a.pop ())

Output:

 Hello Geek Hello Geek Hello Geek 4 3 2 1