The while loop repeats a block of code as long as a condition remains True. Before each iteration, Python checks the condition — if it is True, the body runs; if it is False, the loop stops and execution continues with the next unindented line. You must update the condition inside the loop body (e.g. increment a counter), otherwise the loop will run forever (an infinite loop). While loops are ideal when you do not know in advance how many times the code should repeat.
Key Points
while condition: runs the body as long as the condition is True.
The condition is checked before each iteration, not after.
You must change the condition variable inside the loop to avoid infinite loops.
A counter variable is often initialized before the loop and updated inside it.
while True: creates an intentional infinite loop (stopped with break).
Indentation (4 spaces) defines which lines belong to the loop body.
When the condition becomes False, execution continues past the loop.