What does “while True” mean in Python?

while True means loop forever. The while statement takes an expression and executes the loop body while the expression evaluates to (boolean) “true”. True always evaluates to boolean “true” and thus executes the loop body indefinitely. It’s an idiom that you’ll just get used to eventually! Most languages you’re likely to encounter have equivalent idioms.

Note that most languages usually have some mechanism for breaking out of the loop early. In the case of Python it’s the break statement in the cmd == 'e' case of the sample in your question.

Leave a Comment