Why do Python's 'and' and 'or' have different results depending on how the values are written to the left and right?

Asked 2 years ago, Updated 2 years ago, 300 views

If I enter print(0 or (1 and 2)) in Python, the return value is 2, but if I enter print(0 or (2 and 1))), why is the return value 1?I looked for help, but it didn't work out well.

I've tried many patterns and found out that if it's true, the number on the far right will be returned, but I'd like to know why.

I would appreciate it if you could give me an answer.

python python3

2022-09-30 21:49

2 Answers

Python Language Reference
https://docs.python.org/ja/3/reference/expressions.html#boolean-operations
of
6.11. Boolean operation
to

The expression x and y evaluates x first; if x is false, it returns a value of x; otherwise, it evaluates the value of y and returns the result.

is defined as


2022-09-30 21:49

Answer

If you enter print(0 or (1 and 2)), the return value is 2, but
If print(0 or (2 and 1)), why is the returned value equal to 1?

0 or 0 (false) does not determine the authenticity of the entire expression and the evaluation continues.
The evaluation of (1 and 2) or (2 and 1) following or is
If 1 and 1 is true, the evaluation will not be confirmed and the evaluation will continue.
2 and 2 are also true, so the evaluation will continue as well.
Since the value following and is the last, the evaluation of the entire expression is finalized, and the result is the value following and.

As lehshell replied, Python's grammar 6.11. Boolean operation explains why.

What I checked
It's been quite a long time, if you don't mind, please go out with me.

Let's first look at 1 and 2 and 2 and 1.

>>print(1 and 2)
2
>> print (2 and 1)
1
>> 

The code above appears to show values that follow and .
Try changing the numbers after and and the results are similar.

>>print(1 and 100)
100
>> print (2 and 100)
100

The above code appears to show values that follow and regardless of the previous number.

Try executing the following code:

>>print(0 and 100)
0
>> print (0 and 200)
0

If the number preceding and is 0, 0 is displayed.
In this example, the value following and appears to be 0 regardless.

Now try writing the error expression after and.

>>print(0 and 1/0)
0
>> print (1 and 1/0)
Traceback (most recent call last):
  File "<pyshell#94>", line 1, in <module>
    print(1 and 1/0)
ZeroDivisionError: division by zero
>> print (2 and 1/0)
Traceback (most recent call last):
  File "<pyshell#95>", line 1, in <module>
    print (2 and 1/0)
ZeroDivisionError: division by zero

If the and precedes 0 then no error is displayed, but if the and precedes 0 then no error is displayed.
The difference in behavior seems to be due to the difference in the logical evaluation of the previous value of and.

When I evaluated the number as a logical value (True or False), the results were as follows:

>>bool(0)
False
>>bool(1)
True
>>bool(2)
True

Evaluate the previous value of and as a logical value and
If it is False, the authenticity value of the expression will be determined and the evaluation will be completed.
If it is true, the authenticity value of the expression will not be determined, so it seems that they are evaluating the expression following and .

The evaluation of authenticity will continue until the authenticity is confirmed, and it seems that the result will be the value at the time it is confirmed.

Now I'll try `or'

>>print(0 or 10)
10
>> print (0 or 20)
20
>> print (1 or 10)
1
>> print (1 or 20)
1
>> print (2 or 10)
2
>> print (2 or 20)
2

In this example,
If the previous section of or is false (0), the authenticity of the expression is not determined (may be true in the next evaluation), and the following values are evaluated:
If the previous section of or is true (one or two in this example), the result is a value before or.

In this case, the evaluation of authenticity will continue until the authenticity is confirmed, and the result will be the value at the time it is confirmed.

Summary
The evaluation of the logical expression will continue until the authenticity is confirmed, and once the authenticity is confirmed, the evaluation will end, and the result will be the target of the evaluation.


2022-09-30 21:49

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.