UNISIA-SE Tech Blog

気まぐれお勉強日記

[Python] [基本] 論理演算子まとめ (or, and, not)

1. 前提条件


このページでは、Pythonで使用する論理演算子について基本的な使い方について記載する。

▼ Python3.6がインストールされていること。
このページでは、venvの仮想環境(Python3.6)上にNumPyをインストールした環境で、Python対話モード(Pythonインタプリタ)にて実装サンプルを記載している。

※ Python対話モードについては下記を参考。
Python対話モード:[Python] 対話モード (インタプリタ) の使用方法

2. 論理演算子の種類


論理演算子 には、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) >「dict 型」

▼ 論理演算子一覧
※ 論理演算子は、論理和 or、論理積 and、否定 not の三つのみ。
演算子 使用例 説明
ora or ba、b の論理和
anda and ba、b の論理積
notnot aaの否定

3. True / Falseの判定基準


論理演算で最も重要となる True / False の判定基準ですが 空文字空リスト 等も False と判定される。
下記一覧で示す要素以外は、すべて True として判定される。

▼ False判定一覧
False判定となる要素 説明
Falsebool型のFalse
None何もないことを示すオブジェクト(≒多言語のNull)
0int型(整数)のゼロ
0.0float型(浮動小数点数)のゼロ
0jcomplex型(複素数)のゼロ
Decimal(0)decimal型のゼロ
Fraction(0, 1)fraction型(有理数)のゼロ
''str型(文字列)の空文字
[]list型(配列)の空配列
{}dict型(連想配列)の空配列
()tuple型(タプル)の空配列
set()set型(集合)の空配列
range(0)range型(数値配列)の空配列

※ 各実装サンプルについては、下記ページを参考。
[Python] [基本] 組込みデータ型まとめ (bool, int, float, complex) >「bool 型のFalse判定」


以降、論理演算子に関する簡単な実装サンプルを対話モード(インタプリタ)で解説する。

4. 論理和 ( or )


a or b は、前方から評価していき True となる要素が見つかった時点 (※) でその要素を返す。
a、b 共に Falseである場合は、末尾の要素(b)を返す。

※ この True となる要素が見つかった時点で評価をすることをショートサーキットと呼ぶ。

▼ 論理和パターン
a の評価 b の評価 a or b の評価
TrueFalsea の評価
FalseTrueb の評価
TrueTruea の評価
FalseFalseb の評価

▼ 論理和 実装サンプル
・論理和パターン

$ 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
 >>>

5. 論理積 ( and )


a and b は、前方から評価していき False となる要素が見つかった時点 (ショートサーキット) でその要素を返す。
a、b 共に True である場合は、末尾の要素(b)を返す。

▼ 論理積パターン
a の評価 b の評価 a and b の評価
TrueFalseb の評価
FalseTruea の評価
TrueTrueb の評価
FalseFalsea の評価

▼ 論理積 実装サンプル
・論理積パターン

$ 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
 >>>

6. 論理否定 ( 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の極意』株式会社昭和システム


Copyright UNISIA-SE All Rights Reserved.
s-hama@unisia-se.jp