python 全栈开发:逻辑运算

基础运算符

逻辑运算:

优先级:()> not > and >or

数字转bool值,0为False,非零的数字为True。

1、

print(2 > 1 and 1 < 4 or 2 < 3 and 9 > 6 or 2 < 4 and 3 < 2)
True
print(3 > 4 or 4 < 3 and 1 == 1)
False
print(1 < 2 and 3 < 4 or 1 > 2)
True
print(2 > 1 and 3 < 4 or 4 > 5 and 2 < 1)
True
print(1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8)
False
print(1 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6)
False
print( not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6)
False

2、

x or y ,x为Ture,则输出值为x,x为False,则输出值为y。

x and y ,x为True,则输出值为y,x为False,则输出值为x。

print(0 or 2)
2
print(2 or 0)
2
print(2 and 1)
1
print(1 and 2 )
2
print(0 and 2)
0

 

print(1 or 2)
1

 

posted @ 2019-01-04 01:20  猿猴之家  阅读(194)  评论(0编辑  收藏  举报