Formally, True
is a Python built-in constant of bool type.
You can use Boolean operations on bool types (at the interactive python prompt for example) and convert numbers into bool types:
>>> print not True
False
>>> print not False
True
>>> print True or False
True
>>> print True and False
False
>>> a=bool(9)
>>> print a
True
>>> b=bool(0)
>>> print b
False
>>> b=bool(0.000000000000000000000000000000000001)
>>> print b
True
And there are "gotcha's" potentially with what you see and what the Python compiler sees:
>>> n=0
>>> print bool(n)
False
>>> n='0'
>>> print bool(n)
True
>>> n=0.0
>>> print bool(n)
False
>>> n="0.0"
>>> print bool(n)
True
As a hint of how Python stores bool types internally, you can cast bool types to integers and True will come out to be 1 and False 0:
>>> print True+0
1
>>> print True+1
2
>>> print False+0
0
>>> print False+1
1
But you should not rely on that! It is just interesting. In many C programs, which lacks a bool type, there are commonly these definitions in standard header files to define TRUE and FALSE to be used logically in the C program:
#ifndef FALSE
#define FALSE (0)
#define TRUE (!(FALSE))
#endif
or
#ifndef (TRUE)
#define TRUE (1)
#endif
This is where the True==1
assumption came from I suppose. Python does have a bool type and it would be poor practice to assume any value for True
other than True
.
The more important part of your question is "What is while True
?" And an important corollary: What is false?
First, for every language you are learning, learn what the language considers TRUE and FALSE. Python considers Truth slightly differently than Perl Truth for example. Other languages have slightly different concepts of true / false. Know what your language considers to be True and False for different operations and flow control to avoid many headaches later!
There are many algorithms where you want to process something until you find what you are looking for. Hence the infinite loop or indefinite loop. Each language tend to have its own idiom for these constructs. Here are common C infinite loops, which also work for Perl:
for(;;) { /* loop until break */ }
while (1) {
return if (function(arg) > 3);
}
The while True:
form is common in Python for indefinite loops with some way of breaking out of the loop. Learn Python flow control to understand how you break out of while True
loops. Unlike most languages, for example, Python can have an else
clause on a loop. There is an example in the last link.