目的
この記事では、Pythonで使用する論理演算子について基本的な使い方を記載する。
論理演算子の種類
論理演算子には、bool型に加え、数値型である int型、float型、complex型や文字列である str型、リスト型であるlist型、tuple型、dict型の指定ができる。
-
各データ型の参考
- Python - 組込みデータ型まとめ : bool , int, float, complex > bool型 : 真偽リテラル
- Python - 組込みデータ型まとめ : bool , int, float, complex > int型 : 数値(整数)
- Python - 組込みデータ型まとめ : bool , int, float, complex > float型 : 浮動小数点数型
- Python - 組込みデータ型まとめ : bool , int, float, complex > complex型 : 複素数型
- Python - 組込みデータ型まとめ : str, list, tuple, range, dict > str型 : 文字列型
- Python - 組込みデータ型まとめ : str, list, tuple, range, dict > list型 : 配列型
- Python - 組込みデータ型まとめ : str, list, tuple, range, dict > tuple型 : 定数の配列型
- Python - 組込みデータ型まとめ : str, list, tuple, range, dict > range型 : 範囲指定
-
論理演算子一覧(or, and, not の三つのみ)
演算子 使用例 説明 or a or b a、b の論理和 and a and b a、b の論理積 not not a a の否定
True/Falseの判定基準
論理演算で最も重要となるTrue/Falseの判定基準として、空文字や空リスト等もFalse
と判定される。
下記一覧で示す要素以外は、すべてTrue
として判定される。
- False判定一覧
False判定となる要素 説明 False bool型のFalse None 何もないことを示すオブジェクト(≒多言語のNull) 0 int型(整数)のゼロ 0.0 float型(浮動小数点数)のゼロ 0j complex型(複素数)のゼロ Decimal(0) decimal型のゼロ Fraction(0, 1) fraction型(有理数)のゼロ '' str型(文字列)の空文字 [] list型(配列)の空配列 {} dict型(連想配列)の空配列 () tuple型(タプル)の空配列 set() set型(集合)の空配列 range(0) range型(数値配列)の空配列
※ 各実装サンプルについては、下記ページを参考。
以降、論理演算子に関する簡単な実装サンプルを対話モード(インタプリタ)で解説する。
論理和(or)
a and b
は、前方から評価していきTrue
となる要素が見つかった時点で(ショートサーキットと呼ぶ)その要素を返す。
a、b共にFalseである場合は、末尾の要素b
を返す。
-
論理和パターン
a の評価 b の評価 a or b の評価 True False a の評価 False True b の評価 True True a の評価 False False b の評価 -
論理和 実装サンプル(論理和パターン)
$ python >>> # True or False (int型 or int型) >>> bool_a = 1 or 0 >>> print(bool_a) 1 >>> # False or True (float型 or float型) >>> bool_b = 0.0 or 1.0 >>> print(bool_b) 1.0 >>> # True or True (complex型 or complex型) >>> bool_c = 1j or 2j >>> print(bool_c) 1j >>> # False or False (list型 or dict型) >>> bool_d = [] or {} >>> print(bool_d) {} >>>
-
ショートサーキットの例
一度に複数の論理和を行う場合、ショートサーキットの性質を利用し、Trueになる可能性が高い評価対象を前方に記載することで、不要な演算を省くことができる。$ python >>> # 先頭の 2 (True) のみで評価が返される。 >>> # 以降の 0 (False)、1 (True) は評価しない。 >>> bool_a = 2 or 0 or 1 >>> print(bool_a) 2 >>> # 2項目の 4 (True) で評価が返される。 >>> # 以降の 3 (True)、2 (True)、1 (True) は評価しない。 >>> bool_b = 0 or 4 or 3 or 2 or 1 >>> print(bool_b) 4 >>>
論理積(and)
a and b
は、前方から評価していきFalse
となる要素が見つかった時点(ショートサーキット)でその要素を返す。
a、b共にTrueである場合は、末尾の要素b
を返す。
-
論理積パターン
a の評価 b の評価 a and b の評価 True False b の評価 False True a の評価 True True b の評価 False False a の評価 -
論理積 実装サンプル(論理積パターン)
$ python >>> # True and False (int型 and int型) >>> bool_a = 1 and 0 >>> print(bool_a) 0 >>> # False and True (float型 and float型) >>> bool_b = 0.0 and 1.0 >>> print(bool_b) 0.0 >>> # True and True (complex型 and complex型) >>> bool_c = 1j and 2j >>> print(bool_c) 2j >>> # False and False (list型 and dict型) >>> bool_d = [] and {} >>> print(bool_d) [] >>>
-
ショートサーキットの例 上記の論理和と同様に、一度に複数の論理積を行う場合、ショートサーキットの性質を利用し、Falseになる可能性が高い評価対象を前方に記載することで、不要な演算を省くことができる。
$ python >>> # 先頭の 0 (False) のみで評価が返される。 >>> # 以降の 1 (True)、2 (True) は評価しない。 >>> bool_a = 0 and 1 and 2 >>> print(bool_a) 0 >>> # 3項目の 0 (False) で評価が返される。 >>> # 以降の 4 (True)、5 (True) は評価しない。 >>> bool_b = 1 and 2 and 0 and 4 and 5 >>> print(bool_b) 0 >>>
論理否定(not)
not x
は、対象x
を否定した結果をbool型で返す。
False
ならTrue
を返し、True
ならFalse
を返す。
- 論理否定 実装サンプル
$ python >>> # int型 0 の否定 >>> bool_a = not 0 >>> print(bool_a) True >>> # int型 1 の否定 >>> bool_b = not 1 >>> print(bool_b) False >>> # float型 0.0 の否定 >>> bool_c = not 0.0 >>> print(bool_c) True >>> # float型 2.5 の否定 >>> bool_d = not 2.5 >>> print(bool_d) False >>> # complex型 0j の否定 >>> bool_e = not 0j >>> print(bool_e) True >>> # complex型 1j の否定 >>> bool_f = not 1j >>> print(bool_f) False >>> # int型 1 and int型 2 and int型 0 の否定 >>> bool_g = not (1 and 2 and 0) >>> print(bool_g) True >>>
参考文献
- 金城 俊哉(2018)『現場ですぐに使える! Pythonプログラミング逆引き大全313の極意』株式会社昭和システム