python基础-逻辑运算

逻辑运算

and or not

优先级: ()>not>and>or
print(2>1 and 1<4)
True
print(2 > 1 and 1 < 4 or 2 < 3 and 9 > 6 or 2 < 4 and 3 < 2) ===>print(True or True or False)
True

int —-> bool

非零转换成bool True 、 0 转换成bool 是False
print(bool(2))
print(bool(-2))
print(bool(0))

bool —>int

print(int(True)) # 1
print(int(False)) # 0

x or y ,x为True,则返回x,否则返回y

print(1 or 2) # 1
print(3 or 2) # 3
print(0 or 2) # 2
print(0 or 100) # 100

x and y , x为True,则返回y

print(0 or 4 and 3 or 2) #3
print(1 and 2) #2
print(0 and 2) #0
print(2 or 1 < 3) #True
print(3 > 1 or 2 and 2) #True

发布了5 篇原创文章 · 获赞 3 · 访问量 4581

猜你喜欢

转载自blog.csdn.net/xiaoz2008/article/details/82431534