python3 逻辑运算符

1、and

  "与" ,如果 x 为 False,x and y 返回 False,否则它返回 y 的计算值。 

10<1 and 3>1       #10<1为False,所以整体为False
---------------------------
False
10>1 and 3>1   #10>1为True,所以看3>1的布尔值,3>1为True,则整体值为True.
----------------------------------------------------------------------------------------
True

2、or

  "或",如果 x 是非 0,它返回 x 的值,否则它返回 y 的计算值。

10>1 or 3>1.  #10>1位True,则直接返回10>1的布尔值。
----------------------------------------------------------------
True
10<1 or 3>1.      #因为10<1位False, 则返回3>1的布尔值。
-----------------------------------------------------------------
True

3、not

  "非" - 如果 x 为 True,返回 False 。如果 x 为 False,它返回 True。

not True 
--------------------------
False

4、逻辑运算符的优先级

  从左到右:() > not > and > or

计算:1>3 or 3>2 and 1=0 and not(7>2 or 3>5)

  解答:

  从左到右,先计算()里,7>2为True,则括号里为True,在看括号外面的not,则这一部分#为false.
  再先计算左边的and,3>2为True,则看1=0的布尔值,1=0为False,则3>2 and 1=0为#False.
  再计算 3>2 and 1=0 and not(7>2 or 3>5),已知3>2 and 1=0 为False, not(7>2 or 3>5)为False,则3>2 and 1=0 and not(7>2 or 3>5)为False。
  最后看1>3为False,3>2 and 1=0 and not(7>2 or 3>5)为False,则整体为False.

原文地址:https://www.cnblogs.com/490144243msq/p/11031867.html