Conditional Looping

Python supports conditional looping with the while statement, which has the form:

 

while condition :

    loop-group

Conditional looping executes the body of the loop (the loop-group) as long as the condition evaluates to true.

 

Example: Conditional Looping


# print the first 10 characters

string_1 = “this is a test string”

i = 0

while i < 10 : 

    print string_1[i]

    i += 1