loops python offers two distinct looping constructs while loop allows general repetition based upon the repeated testing of boolean condition for loop provides convenient iteration of values from defined series (such as characters of stringelements of listor numbers within given rangewe discuss both forms in this section while loops the syntax for while loop in python is as followswhile conditionbody as with an if statementcondition can be an arbitrary boolean expressionand body can be an arbitrary block of code (including nested control structuresthe execution of while loop begins with test of the boolean condition if that condition evaluates to truethe body of the loop is performed after each execution of the bodythe loop condition is retestedand if it evaluates to trueanother iteration of the body is performed when the conditional test evaluates to false (assuming it ever does)the loop is exited and the flow of control continues just beyond the body of the loop as an examplehere is loop that advances an index through sequence of characters until finding an entry with value or reaching the end of the sequence = while len(dataand data[ ! + the len functionwhich we will introduce in section returns the length of sequence such as list or string the correctness of this loop relies on the shortcircuiting behavior of the and operatoras described on page we intentionally test len(datato ensure that is valid indexprior to accessing element data[jhad we written that compound condition with the opposite orderthe evaluation of data[jwould eventually raise an indexerror when is not found (see section for discussion of exceptions as writtenwhen this loop terminatesvariable ' value will be the index of the leftmost occurrence of if foundor otherwise the length of the sequence (which is recognizable as an invalid index to indicate failure of the searchit is worth noting that this code behaves correctlyeven in the special case when the list is emptyas the condition len(datawill initially fail and the body of the loop will never be executed