Python语言支持以下逻辑运算符。假设变量a
的值为True
,变量b
的值为False
,那么 -
运算符 | 描述 | 示例 |
---|---|---|
and |
如果两个操作数都为真,则条件成立。 | (a and b) 的结果为False |
or |
如果两个操作数中的任何一个非零,则条件成为真。 | (a or b) 的结果为True |
not |
用于反转操作数的逻辑状态。 | not(a and b) 的结果为True 。 |
实例
#! /usr/bin/env python
#coding=utf-8
# save file: logical_operators_example.py
a = True
b = False
not(a and b)
print ('(a and b) = ', (a and b))
print ('(a or b) = ', (a or b))
print ('not(a and b) = ', not(a and b))
将上面代码保存到文件: logical_operators_example.py 中,执行结果如下 -
F:worksppython>python logical_operators_example.py
(a and b) = False
(a or b) = True
not(a and b) = True
上一篇:
Python基本运算符
下一篇:
Python决策