Is False == 0 and True == 1 an implementation detail or is it guaranteed by the language?

convert true false to 1 0 python
boolean true false == 0 1
is the value -16 interpreted as true or false?
convert true false to 1 0 python pandas
true false to 0 1
python true false
convert true to 1 and false to 0
python distinguish between 0 and false

Is it guaranteed that False == 0 and True == 1, in Python (assuming that they are not reassigned by the user)? For instance, is it in any way guaranteed that the following code will always produce the same results, whatever the version of Python (both existing and, likely, future ones)?

0 == False  # True
1 == True   # True
['zero', 'one'][False]  # is 'zero'

Any reference to the official documentation would be much appreciated!

Edit: As noted in many answers, bool inherits from int. The question can therefore be recast as: "Does the documentation officially say that programmers can rely on booleans inheriting from integers, with the values 0 and 1?". This question is relevant for writing robust code that won't fail because of implementation details!


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.6 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.6 and 3.

So you're safe until Python 4 comes along. ;-)

Boolean data type, In computer science, the Boolean data type is a data type that has one of two possible values Indeed, a Boolean variable may be regarded (and implemented) as a In some languages, like Ruby, Smalltalk, and Alice the true and false values The BIT data type, which can only store integers 0 and 1 apart from NULL,  This question is relevant for writing robust code that won't fail because of implementation details! 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.


Link to the PEP discussing the new bool type in Python 2.3: http://www.python.org/dev/peps/pep-0285/.

When converting a bool to an int, the integer value is always 0 or 1, but when converting an int to a bool, the boolean value is True for all integers except 0.

>>> int(False)
0
>>> int(True)
1
>>> bool(5)
True
>>> bool(-5)
True
>>> bool(0)
False

Programming Language Implementation and Logic Programming: 6th , For p, q positions, p == q is true iff terms at positions p, q are equal. For example, on Figure 1 var(po) is false, var(p^ is true, functor(po) = P, pz == p$ is true and automated deduction.5 The algorithm match tries to construct a substitution 0  En Python 2.x ceci est et non garanti car il est possible de réassigner True et False. Cependant, même si cela se produit, Boolean True et Boolean False sont encore correctement retournés pour des comparaisons. En Python 3.x True et False sont des mots clés et seront toujours égaux à 1 et 0.


In Python 2.x, it is not guaranteed at all:

>>> False = 5
>>> 0 == False
False

So it could change. In Python 3.x, True, False, and None are reserved words, so the above code would not work.

In general, with booleans you should assume that while False will always have an integer value of 0 (so long as you don't change it, as above), True could have any other value. I wouldn't necessarily rely on any guarantee that True==1, but on Python 3.x, this will always be the case, no matter what.

Microprogrammed State Machine Design, IF (REG OF == TRUE) {BA = REG; } This statement can defer the control of an While we could describe a controller using the language introduced above, we find that the clarity provided by submerging this detail under a common set of equal to TRUE or 1, i.e., A is FALSE A == FALSE A is False or 0 A = FALSE A is  = True F A = False F A = True F A = False A = True 0 0 0 1 A = False 1 1 1 0 d. Suppose the alarm and gauge are working and the alarm sounds. Calculate an expression for the probability that the temperature of the core is too high, in terms of the various conditional probabilities in the network.


Very simple. As bool relates to evaluating an integer as a bool, ONLY zero gives a false answer. ALL Non-Zero values, floats, integers, including negative numbers, or what have you, will return true.

A nice example of why this is useful is determining the power status of a device. On is any non-zero value, off is zero. Electronically speaking this makes sense.

To determine true or false relatively between values, you must have something to compare it to. This applies to strings and number values, using == or != or <, > >=, <=, etc.

You can assign an integer to a variable and then get true or false based on that variable value.

Implementation and Applications of Automata: 13th International , Boolean domain B = {false, true} forms the Boolean algebra (B, =>, V, A, 5, false, true). A Heyting algebra (H, s, Ll, T, = , 0, 1) is a lattice where (1) 0 < a. and a < 1 for all A language is a subset of X"; an w-language is a subset of XX"; and an  Traditionally, we identify True with 1 and False with 0. We identify AND with * and XOR with +. Thus OR is saturating addition. However, we could just as easily identify True with 0 and False with 1. Then we identify OR with * and XNOR with +. Thus AND is saturating addition.


Just write int(False) and you will get 0, if you type int(True) it will output 1

Conditionals with if/else & Booleans | AP CSP (article), However we code it, the condition needs to evaluate to either true or false , because that tells the There are many comparison operators in programming languages to determine different 32. == Equality, 32 == 32, "32" == 32 Our program is guaranteed to always go down one of the two paths: either the condition is true  true and false operators (C# reference) The true operator returns the bool value true to indicate that an operand is definitely true. The false operator returns the bool value true to indicate that an operand is definitely false. The true and false operators are not guaranteed to complement each other.


Implementation and Application of Functional Languages: 16th , Corecursive Constant Initialization (Example) 1. distinguish between recursive and corecursive function def all0 == (0,all0) recycle ditto tuple digits // create new tuple cell C, push reference to C dup // copy (a) (b) (c) = false true ≤ true true < [True/False] The factors that most influence the success of an information system are the functionality of the system, the quality of the data, the usefulness of the system, system usage, system efficiency, and user satisfaction.


Programming Languages: Implementations, Logics, and Programs: 8th , and(t,s) for(t, s)), according to these rules, if the first argument reduces to false (​true), By imposing the replacement map /i(and) = M(or) = 0}> we avoid the evaluation of the head of the list and the second one evaluates the rest of the list. True or False? Setting the fieldwidth of a data item holds only for the next consecutive item to be output. After this output, the fieldwidth resets to 0.


4. Built-in Types, Return True if the float instance is finite with integral value, and False otherwise: CPython implementation detail: Currently, the prime used is P = 2**31 - 1 on machines with 32-bit C longs while m % P == n % P == 0: m, n = m // P, n // P if n % P == 0: hash_ (For full details see Comparisons in the language reference.​). False. Pseudocode is normal English and can be written on paper or in any text editor. True or False: Pseudocode must be written in an editor capable of understanding it and the language to which it pertains.