Logic operations in Python 2.7

Asked

Viewed 296 times

3

When performing logical operations in Python 2.7, I noticed something strange. For example, when doing 5 and 6, are obtained as a result 6. Now, the correct would be 4, because, converting these values to binary, we have 101 and 110, that 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 value 1. Why this "strange" behavior? There is something missing in this operation?

  • 2

    The logical operator and is different from &, who is the operator and bit by bit. If you do print(5 & 6), the output will be 4 as expected.

2 answers

5

The operator and treats your operands as boolean. That is, when you do:

5 and 6

The Python interpreter will do the cast from operands to bool and then check the result. Something like bool(5) and bool(6), but not quite that. What actually happens has to do with short-circuiting in logical expressions. That is, Python will check the first operand by checking the result of bool(5); if true and the operation is and, the final result will depend only on the second operand, this being returned, because True and X will always be X, independent of its value. If you change the operator to or, that is, do 5 or 6, the same logic occurs, however, as bool(5) is treated as true, the short circuit of the expression happens and is returned the value 5, because True or X is always true, independent of X.

The logic you described expecting the result 4 is known as bit-by-bit logic. To do so, you will need to use the operator & for bitwise and or | for bitwise or. That is, the result 4, which is expected, can be obtained by:

print(5 & 6)

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


To complete the reply of the LINQ, which in turn supplements this, the equivalent (unofficial) code of the operator and 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’ll see that the exit is True.

Already to the operator or:

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-mentioned answer, the return of the operator is not necessarily of the boolean type, because the returned values are x or y, and in this way, the returned type will be the type of x or of y, depending on which one was returned by the operator. Fact that can be proven by making print(type(5 and 6)), resulting in <class 'int'>, or print(type([] and 6)) which results in <class 'list'>.

  • Thank you, Anderson! Your reply also helped me.

3


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

Using the and this is exactly the expected behavior. He is a logical operator, has similar functioning to &, but not quite the same.

Python’s own documentation 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.

In free translation:

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

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

Trying to explain more calmly.

Well, let’s see, x and y will only return True if the two expressions are true, right? If so, if x for False there is no need to evaluate the expression y, because either way the result will be False.

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

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


Perhaps it is interesting to read this question I asked. For you to locate yourself, the && C# is like the and python. And the & is the same for both.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.