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
moyutime:本文仅是学习心得,观点仅供参考,祝愿读者学习途中快乐且不断有所收获。