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決策
