syntax – What does while True mean in Python?
syntax – 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. Its an idiom that youll just get used to eventually! Most languages youre 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 its the break
statement in the cmd == e
case of the sample in your question.
my question: while WHAT is True?
While True
is True
.
The while loop will run as long as the conditional expression evaluates to True
.
Since True
always evaluates to True
, the loop will run indefinitely, until something within the loop return
s or break
s.
syntax – What does while True mean in Python?
while True
is true — ie always. This is an infinite loop
Note the important distinction here between True
which is a keyword in the language denoting a constant value of a particular type, and true which is a mathematical concept.