The while Statement

The while statement repetitively executes a suite until a condition is met. Its basic form is

while( expression ):
      suite

which you can think of in English as “While the expression remains true, execute the suite repeatedly.” Python carries out this instruction by returning to the top of the statement to check if the expression is still true; if so, it executes the suite a second time and once again returns to the top (this is referred to as looping), repeating this process until the expression becomes false. Once that happens, Python stops executing, and the while statement's execution comes to a halt.

One use of the while statement is to remove items from a list—for example, every occurrence of the string cat from the list ...

Get Python Programming with the Java™ Class Libraries: A Tutorial for Building Web and Enterprise Applications with Jython now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.