Pythonの論理演算子and, or, not(論理積、論理和、否定)

Modified: | Tags: Python

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 > ornotが最も高い)。

以下のサンプルコードでは、上の式は下の式のように解釈される。余分な括弧()があっても問題はないので、この例のような場合は明確に記述したほうが分かりやすいかもしれない。

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の論理演算において、偽と判定されるのは以下のオブジェクト。

これ以外はすべて真と判定される。

関数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を返す)。例は数値だが文字列やリストなど他の型でも同様。

andorの返り値の定義は以下の通り。

x and y は、まず x を評価します; x が偽なら x の値を返します; それ以外の場合には、 y の値を評価し、その結果を返します。

x or y は、まず x を評価します; x が真なら x の値を返します; それ以外の場合には、 y の値を評価し、その結果を返します。
6. 式 (expression) - ブール演算 (boolean operation) — Python 3.12.0 ドキュメント

andorの返り値を表にまとめると以下のようになる。

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 yxが偽の場合やx or yxが真の場合は、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

関連カテゴリー

関連記事