Pythonの論理演算子and, or, not(論理積、論理和、否定)
Pythonの論理演算子and
(かつ・論理積), or
(または・論理和), not
(でない・否定)の使い方について説明する。if文で複数の条件の関係を記述する際などに使う。
True
, False
の論理演算(ブール演算)ではなく、2進数で表した整数の各ビットに対する論理演算(ビット演算)については以下の記事を参照。and
, or
ではなく&
, |
を使う。
if文についての詳細は以下の記事を参照。
論理積(かつ): and
and
は二つの値の論理積(かつ)を返す。
print(True and True)
# True
print(True and False)
# False
print(False and True)
# False
print(False and False)
# False
実際にはTrue
, False
に対してではなく、比較演算子(<
, >
など)による条件式などに対して用いることが多い。or
, not
でも同様。
a = 10
print(0 < a)
# True
print(a < 100)
# True
print(0 < a and a < 100)
# True
複数の比較のand
は以下のように連結して記述できる。
print(0 < a < 100)
# True
論理和(または): or
or
は二つの値の論理和(または)を返す。
print(True or True)
# True
print(True or False)
# True
print(False or True)
# True
print(False or False)
# False
否定(でない): not
not
は値の否定(でない)を返す。True
, False
が反転する。
print(not True)
# False
print(not False)
# True
and, or, not演算子の優先順位
これらの論理演算子の優先順位は、not
> and
> or
(not
が最も高い)。
以下のサンプルコードでは、上の式は下の式のように解釈される。余分な括弧()
があっても問題はないので、この例のような場合は明確に記述したほうが分かりやすいかもしれない。
print(True or True and False)
# True
print(True or (True and False))
# True
and
より先にor
を演算したい場合は括弧()
を使う。
print((True or True) and False)
# False
<
, >
などの比較演算子はnot
よりもさらに優先順位が高いため、上の例にもあったようにそれぞれの比較演算に括弧()
は必要ない。
print(0 < a and a < 100)
# True
Pythonにおける演算子の優先順位のまとめは以下の公式ドキュメントを参照。
bool型以外のオブジェクトに対する論理演算子
論理演算子and
, or
, not
では、bool型(True
, False
)だけでなく、数値や文字列、リストなども真偽値として判定される。
Pythonの論理演算において、偽と判定されるのは以下のオブジェクト。
- 偽であると定義されている定数:
None
とFalse
.- 数値型におけるゼロ:
0
,0.0
,0j
,Decimal(0)
,Fraction(0, 1)
- 空のシーケンスまたはコレクション:
''
,()
,[]
,{}
,set()
,range(0)
組み込み型 - 真理値判定 — Python 3.12.0 ドキュメント
これ以外はすべて真と判定される。
関数bool()
を使うとオブジェクトをbool
型に変換できる。文字列'0'
や'False'
はTrue
に変換されるので注意。
print(bool(10))
# True
print(bool(0))
# False
print(bool(''))
# False
print(bool('0'))
# True
print(bool('False'))
# True
print(bool([]))
# False
print(bool([False]))
# True
文字列'0'
や'False'
を偽として処理するにはdistutils.util.strtobool()
を使う。以下の記事を参照。
and, orの返り値はbool型とは限らない
bool
型以外のオブジェクトの例として、数値に対するand
, or
, not
の結果を示す。
x = 10 # Evaluated as true
y = 0 # Evaluated as false
print(x and y)
# 0
print(x or y)
# 10
print(not x)
# False
上の例から分かるように、Pythonのand
, or
は、bool型のTrue
, False
を返すのではなく、真偽に応じて左側または右側の値を返す(not
はbool型のTrue
, False
を返す)。例は数値だが文字列やリストなど他の型でも同様。
and
とor
の返り値の定義は以下の通り。
式
x and y
は、まずx
を評価します;x
が偽ならx
の値を返します; それ以外の場合には、y
の値を評価し、その結果を返します。式
x or y
は、まずx
を評価します;x
が真ならx
の値を返します; それ以外の場合には、y
の値を評価し、その結果を返します。
6. 式 (expression) - ブール演算 (boolean operation) — Python 3.12.0 ドキュメント
and
とor
の返り値を表にまとめると以下のようになる。
x | y | x and y | x or y |
---|---|---|---|
真 | 偽 | y | x |
偽 | 真 | x | y |
真 | 真 | y | x |
偽 | 偽 | x | y |
if文の条件式などで使う場合は結果が真偽値として判定・処理されるので特に気にする必要はないが、返り値を使ってさらに処理を行う場合は注意が必要。
x = 10 # Evaluated as true
y = 100 # Evaluated as true
print(x and y)
# 100
print(y and x)
# 10
print(x or y)
# 10
print(y or x)
# 100
x = 0 # Evaluated as false
y = 0.0 # Evaluated as false
print(x and y)
# 0
print(y and x)
# 0.0
print(x or y)
# 0.0
print(y or x)
# 0
print(bool(x and y))
# False
True
, False
として扱いたい場合は最後の例のようにbool()
を使えばよい。
ショートサーキット(短絡評価)
x and y
でx
が偽の場合やx or y
でx
が真の場合は、y
の値によらず返り値がx
となる。このような場合、y
は評価されない。
and
, or
の右側で関数やメソッドを呼び出して何らかの処理を行う場合は、左側の結果によって処理が実行されないことがあるので注意。
def test():
print('function is called')
return True
print(True and test())
# function is called
# True
print(False and test())
# False
print(True or test())
# True
print(False or test())
# function is called
# True