Is while True !=0: valid , if so what does it mean? Because in my program using that or "while True" (by itself) provides the same output. I can't remember why I used it before in my code.
Thanks in advance.
Is while True !=0: valid , if so what does it mean? Because in my program using that or "while True" (by itself) provides the same output. I can't remember why I used it before in my code.
Thanks in advance.
This is used to create infinite loops (or loops that exit via a break
or return
statement within the loop body). The standard way to do this is:
while True:
...
The other version you showed, while True != 0:
, is equivalent, but needlessly convoluted. It works because, when treated as an integer, True
has the value 1
, so it's equivalent to while 1 != 0:
which, in turn, is equivalent to while True:
, which is the most direct way to express it.
Yes, while True != 0:
is valid and it has the same meaning as while True:
which you should use from now on.
This works because int(True)
evaluates to 1
.