Python:bool运算符的行为

bool运算符的行为

操作符 操作定义
x and y 如果x为假,返回x,否则返回y
x or y 如果x为真,返回x,否则返回y
not x 如果x为假,返回真,否则返回假

例子

a = 1
b = 2
c = a and b
print(c)
# 2

a = 0
b = 2
c = a and b
print(c)
# 0

a = 1
b = 2
c = a or b
print(c)
# 1

a = 0
b = 2
c = a or b
print(c)
# 2

a = 1
c = not a
print(c)
# False
posted on 2021-01-24 14:55  摸鱼time  阅读(76)  评论(0编辑  收藏  举报