Logical operations in Python 2.7

2

When performing logical operations in Python 2.7, I noticed something strange. For example, doing 5 and 6 results in 6 . Now, the correct one would be 4, since converting these values to binary, we have 101 and 110 , which would give 100 , because in logical operations "and", the result is only true, ie 1 , when their entries are also true, that is, they have 1 value. Why this "strange" behavior? Is there anything missing from this operation?

    
asked by anonymous 21.06.2017 / 15:17

2 answers

3

What you intend to use is the & operator, which is the bit operator. It does the operation the way you described in your question.

Using and this is exactly the expected behavior. It is a logical operator , it works like & , but not quite the same.

The Python documentation itself explains this:

  

The expression x and y first evaluates x ; if x is false, its value is returned; otherwise, y is evaluated and the resulting value is returned.

     

Note that neither and nor or restrict the value and type they return to False and True, but rather return the last evaluated argument.

Free translation:

  

The expression x and y first evaluates x ; if x is false, its value is returned; otherwise, y is evaluated and the resulting value is returned.

     

Note that neither and nor or restricts values or types returned to False and True , instead they return the last evaluated argument.

Trying to explain it more calmly.

Well, let's see, x and y will only return True if both expressions are true, right? Therefore, if x is False it is not necessary to evaluate the expression y , because, in any case, the result will be False .

Now, if x is True , the y expression also needs to be evaluated and the expression return will be the y value. Which makes complete sense because if the left side of the expression is True who decides the "result" of it is always the rightmost value, if it is True the result will be True and if it is False the result will be False too.

In its specific case the following occurs, the first expression is evaluated, since it is not False , the second is evaluated and its value is returned.

It may be interesting to read this question that I asked. For you to locate, && of C # is like and of Python. And & is equal for both.

21.06.2017 / 15:33
4

The and operator treats its operands as booleans. This is when you do:

5 and 6

The Python interpreter will cast the cast of the operands to bool and then check the result. Something like bool(5) and bool(6) , but not exactly that. What happens in fact is related to the short circuit in logical expressions. That is, Python will check the first operand, checking the result of bool(5) ; if true and the operation is and , the final result will depend only on the second operand, which is returned, since True and X will always X , regardless of its value. If you change the operator to or , that is, to 5 or 6 , the same logic occurs, however, since bool(5) is treated as true, the expression short circuit happens and the value 5 is returned because True or X is always true, independent of X .

The logic you described expecting output 4 is known as bitwise logic. To do this, you must use the & operator for bitwise and or | to bitwise or . That is, the result 4, which is expected, can be obtained by doing:

print(5 & 6)

Or the value 7 when doing print(5 | 6) , because 101 | 110 results in 111 .

To complete LINQ's response , which in turn completes that, the equivalent (unofficial) of the and operator is:

def _and_ (x, y):

    # Interpreta o valor de x como booleano:
    eval_x = bool(x)

    # Se for falso:
    if not eval_x:

        # Retorna o valor de x:
        return x

    # Caso contrário, retorna y:
    return y

If you do print(5 and 6 == _and_(5, 6)) you will see that the output is True .

Already for the or operator:

def _or_ (x, y):

    # Interpreta o valor de x como booleano:
    eval_x = bool(x)

    # Se for verdadeiro:
    if eval_x:

        # Retorna o valor de x:
        return x

    # Caso contrário, retorna y:
    return y

As commented on the above response, the return of the operator is not necessarily of the Boolean type, since the returned values are x or y , and thus the returned type will be the type of x or y , depending on which was returned by the operator. This fact can be proven by doing print(type(5 and 6)) , resulting in <class 'int'> , or print(type([] and 6)) which results in <class 'list'> .

    
21.06.2017 / 15:31