An Introduction to Python For Undergraduate Engineers/While Loops

While loops will repeat a section of code whilst a logical test is true.

Instead of repeating something with a defined list of values or a set number of times, a while loop will happily continue repeating its section of code until a logical test is satisfied. Programs that run forever are to stop with Restart Shell in the IDLE or to hit the Control (or Ctrl) button and C (the letter) at the same time. For example:

The same code could also be written using != which means NOT EQUAL TO, for example: