Is False == 0 and True == 1 an implementation detail or is it guaranteed by the language?
In Python 2.x this is not guaranteed as it is possible for True
and False
to be reassigned. However, even if this happens, boolean True and boolean False are still properly returned for comparisons.
In Python 3.x True
and False
are keywords and will always be equal to 1
and 0
.
Under normal circumstances in Python 2, and always in Python 3:
False
object is of type bool
which is a subclass of int
:
object
|
int
|
bool
It is the only reason why in your example, ['zero', 'one'][False]
does work. It would not work with an object which is not a subclass of integer, because list indexing only works with integers, or objects that define a __index__
method (thanks mark-dickinson).
Edit:
It is true of the current python version, and of that of Python 3. The docs for python 2 and the docs for Python 3 both say:
There are two types of integers: [...] Integers (int) [...] Booleans (bool)
and in the boolean subsection:
Booleans: These represent the truth values False and True [...] Boolean values behave like the values 0 and 1, respectively, in almost all contexts, the exception being that when converted to a string, the strings "False" or "True" are returned, respectively.
There is also, for Python 2:
In numeric contexts (for example when used as the argument to an arithmetic operator), they [False and True] behave like the integers 0 and 1, respectively.
So booleans are explicitly considered as integers in Python 2 and 3.
So you're safe until Python 4 comes along. ;-)
Is it guaranteed that False is 0 and True is 1?
It is part of the language specification, so any Python implementation should implement the booleans as equivalent to the integers.
Booleans
These represent the truth values False and True. The two objects representing the values False and True are the only Boolean objects. The Boolean type is a subtype of plain integers, and Boolean values behave like the values 0 and 1, respectively, in almost all contexts, the exception being that when converted to a string, the strings
"False"
or"True"
are returned, respectively.
Why True value returns True with membership in function in python
True
is implictly converted to a number, 1
. (False
would be converted to 0
) The in
operator checks if any element is equal to the left operand, and True
is equal to the first element, 1
.
>>> True == 1
True
Why can I use True or False as indexes of a list?
Since bool
is a subclass of int
that's why True
acts like 1 and False
like 0
Python bool + bool returns value 2
input()
in Python 3 returns a string. No data conversions are performed. So the inputs you get are, literally, the strings "True"
and/or "False"
. Then bool()
applied to a non-empty string returns True
.
>>> bool("True")
True
>>> bool("False")
True
>>> bool("just about anything")
True
>>> bool("") # except an empty string
False
That's why you always get 2 no matter what you enter. Both inputs become True
, which equals 1 in a numeric context.
>>> True == 1
True
While people will yell at you if you do this, eval()
is the most convenient (although dangerous!) way to evaluate a string as if it were Python code:
>>> eval("True")
True
>>> eval("False")
False
It's dangerous because eval()
will evaluate any Python expression, so if you don't trust your input it can trick your program into doing just about anything.
How can you add and multiple to True or False?
The answer is 5.25 because in Python, True is equal to 1 while False is equal to 0.
Related Topics
Why Does Multiprocessing Use Only a Single Core After I Import Numpy
Extracting Text from Ms Word Files in Python
How to Change the Environment of a Parent Process in Python
How to Set the 'Backend' in Matplotlib in Python
Pass Input/Variables to Command/Script Over Ssh Using Python Paramiko
Shebang Notation: Python Scripts on Windows and Linux
How to Find the Real User Home Directory Using Python
How to Get Pid by Process Name
Python: How to Kill Child Process(Es) When Parent Dies
Run Python Script At Startup in Ubuntu
Compare Two Images the Python/Linux Way
How to Read a Single Character from the User
Adding a Scrollbar to a Group of Widgets in Tkinter
What Does the 'B' Character Do in Front of a String Literal
Pygame Installation Issue in MAC Os
Is It Pythonic to Use List Comprehensions For Just Side Effects