Skip to content Skip to sidebar Skip to footer

"4 And 5" Is 5, While "4 Or 5" Is 4. Is There Any Reason?

When I test the difference between and and or, I meet this problem. Could you please help me understand it?

Solution 1:

This behavior is a weird quirk that comes out of three different features of python code. Non-zeros values are true, logical operation evaluation, and short-circuiting. I'll explain these features below:

Non-Zero Values are True

The first thing you have to know in evaluating these expressions is that they are logical operators. They are designed to work with true or false values:

true and true = true
true and false = false
false and true = false
false and false = false

true or true = true
true or false = true
false or true = true
false or false = false

However, python (and many languages) allow you to put any value in. So long as they are non-zero they are considered true. So:

4 and 5 = true
4 and 0 = false

This is normal so far. Most languages have this.

Logical Operation Evaluation

Here Python does something a little unique. Instead of returning true or false, it actually returns the value of the last item it checked in the statement. So:

4 and 5 = 5 (which will be evaluated as true)

To fully understand which value will actually get returned you have to also understand:

Short-Circuiting

When evaluating these logical operators, the compiler can often stop early. Take the example:

3 or 4

We know that the statement will return true, but which value will it return? To figure this out you have to understand which value will be the last one looked at. The system will look at 3, and realize that the statement is true. It doesn't matter what the second value is, 3 or anything is true. So the value returned is 3 because it is the last value checked.

However, if we use and:

3 and 4

Once we look at 3, we still need to check that the second value is true. It could makes a difference. So the second value is evaluated. If it is true, it returns the last value looked at, in this case 4.

In Conclusion

You just have to think about which value the interpreter can stop on.

3 or 4 = 3 // because we can stop at 3
3 and 4 = 4 // because we have to check the 4
0 or 3 = 3 // because we needed to check the 3

Solution 2:

Yes, the and operator requires all arguments to be true, and returns the last one checked, which is 5. (If any of the arguments were false, it would return the first false value, since that would be the last one checked in order to verify if all arguments were true.)

The or operator requires only one argument to be true, and returns the last one checked, which is 4, because 4 represents the first true value in the conditional. (If all arguments were false, then the return value would be equal to the last false value, since that would be the last value checked in order to verify if any of the arguments were true.)


Solution 3:

true1 and true2 >>>true2
true1 or true2 >>>true1

when runinng true1 and true2, python must check the value returned by every expression is true or not, so it will return the last one.

but when running true1 or true2 , as true1 retrun "true" (in your example, 4 is "true") already, so it is unncessary to continue checking the rest.


Solution 4:

I think the way to look at it is at a simpler level, which was designed for optimization.

and requires that both sides be "truthy." It checks the left side. If it is "truthy," it returns the second value without checking what it is.

or requires only one side to be "truthy." It checks the first side. If it is "truthy," it returns it. If not, it returns the second side, again without checking it.

For "4 and 5", because 4 is "truthy," it returns 5. For "4 or 5", it returns 4 without even looking at 5.

Need proof? Make these functions:

def four():
    print 'Evaluating four!'
    return 4

def five():
    print 'Evaluating five!'
    return 5

Now see what gets printed:

>>> four() and five()
Evaluating four!
Evaluating five!
5

and evaluated four(), and since it was true it returned five().

>>> left() or right()
Evaluating left side!
4

or evaluated four() and since it was true, returned four() without even calling five()!


Post a Comment for ""4 And 5" Is 5, While "4 Or 5" Is 4. Is There Any Reason?"