Python演算子完全ガイド:算術、論理、比較、代入、ビットワイズ、優先順位
Pythonにおける論理演算子とは
Pythonの論理演算子はブール式を評価し、TrueまたはFalseを返します。主要な論理演算子はand、or、notの3つです。これらはif、whileなどの条件文でのフロー制御に不可欠です。
このチュートリアルでは、Python演算子の全スペクトルを探ります:
- 算術演算子
- 比較演算子
- 代入演算子
- 論理 & ビットワイズ演算子
- メンバーシップ演算子
- アイデンティティ演算子
- 演算子優先順位
算術演算子
算術演算子は数値計算を行います。Pythonは通常の演算子をサポート: +(加算)、-(減算)、*(乗算)、/(真の除算)、//(切り捨て除算)、%(剰余)、**(累乗)。これらは式で直接使用するか、動的評価のために組み込みeval()関数経由で使用できます。
例: 単純な加算
x = 4 y = 5 print(x + y) # 出力: 9
他の演算子も同様: 4 * 5は20、4 / 2は2.0、4 ** 2は16。
比較演算子
比較(関係)演算子は2値の関係を評価し、ブール結果を返します。含まれるもの:
==(等しい)!=(等しくない)<(未満)<=(以下)>(超過)>=(以上)
例: 2変数の比較
x = 4
y = 5
print('x > y is', x > y) # 出力: False
代入演算子
代入演算子は変数に値を格納します。基本形は=、複合演算子は演算と代入を組み合わせたもので、+=、-=、*=、/=、**=などがあります。
例: 基本と複合代入
num1 = 4
num2 = 5
print('Line 1 - Value of num1:', num1)
print('Line 2 - Value of num2:', num2)
# 複合加算
res = num1 + num2
res += num1
print('Line 3 - Result after res += num1:', res) # 出力: 13
論理演算子とビットワイズ演算子
論理演算子(and、or、not)はブール式を評価。ビットワイズ演算子(&、|、^、~、<<、>>)はビットレベル操作を行います。
例: 論理演算
a = True
b = False
print('a and b is', a and b) # 出力: False
print('a or b is', a or b) # 出力: True
print('not a is', not a) # 出力: False
メンバーシップ演算子
メンバーシップ演算子はシーケンス内の値の存在をテスト: inとnot in。
例: リスト内のメンバーシップチェック
x = 4
y = 8
sample_list = [1, 2, 3, 4, 5]
if x in sample_list:
print('Line 1 - x is available in the list')
else:
print('Line 1 - x is not available in the list')
if y not in sample_list:
print('Line 2 - y is not available in the list')
else:
print('Line 2 - y is available in the list')
アイデンティティ演算子
アイデンティティ演算子はオブジェクトのメモリアドレスを比較: isとis not。値が等しくても別オブジェクトである場合を区別する際に重要です。
例: アイデンティティ比較
x = 20
y = 20
if x is y:
print('x & y have the same identity')
y = 30
if x is not y:
print('x & y have different identities')
演算子優先順位
Pythonは括弧なしで式を解決するための厳格な優先順位階層に従います。最高は累乗(**)、次に単項演算子、乗除算/剰余、加減算、比較演算子、アイデンティティ演算子、メンバーシップ演算子、論理演算子、最後に代入演算子の順です。
括弧を使えばデフォルト優先順位を上書きし、式を明示的にできます。
例: 優先順位の動作
v = 4
w = 5
x = 8
y = 2
z = (v + w) * x / y
print('Value of (v + w) * x / y is', z) # 出力: 28.0
Python 2互換性
以下はPython 2用の等価スニペットで、print文は括弧なしです。
# 算術演算子
x = 4
y = 5
print x + y
# 比較演算子
x = 4
y = 5
print 'x > y is', x > y
# 代入演算子
num1 = 4
num2 = 5
print 'Line 1 - Value of num1:', num1
print 'Line 2 - Value of num2:', num2
# 複合代入
num1 = 4
num2 = 5
res = num1 + num2
res += num1
print 'Line 3 - Result after res += num1:', res
# 論理演算子
a = True
b = False
print 'a and b is', a and b
print 'a or b is', a or b
print 'not a is', not a
# メンバーシップ演算子
x = 4
y = 8
sample_list = [1, 2, 3, 4, 5]
if x in sample_list:
print 'Line 1 - x is available in the list'
else:
print 'Line 1 - x is not available in the list'
if y not in sample_list:
print 'Line 2 - y is not available in the list'
else:
print 'Line 2 - y is available in the list'
# アイデンティティ演算子
x = 20
y = 20
if x is y:
print 'x & y have the same identity'
y = 30
if x is not y:
print 'x & y have different identities'
# 演算子優先順位
v = 4
w = 5
x = 8
y = 2
z = (v + w) * x / y
print 'Value of (v + w) * x / y is', z
まとめ
- 算術演算子は数値計算を処理
- 比較演算子は関係的真偽を決定
- 代入演算子は値を格納;複合型は演算と組み合わせ
- 論理演算子はフロー制御を統括;ビットワイズ演算子はビット操作
- メンバーシップ演算子はシーケンス包含をテスト
- アイデンティティ演算子はオブジェクト同一性を比較
- 演算子優先順位は評価順を規定;括弧で上書き可能
Python
- C# ビット演算子とビットシフト演算子をマスターする:包括的ガイド
- C 言語ビット演算子マスター: AND、OR、XOR、シフト、補数
- Python 変数・定数・リテラル: 包括的ガイド
- Python データ型マスター: 実践ガイド
- Python 演算子完全マスター: 包括的ガイド
- Python 関数引数完全マスター: 位置引数・キーワード引数・デフォルト引数
- Python辞書完全ガイド:作成、操作、高度なテクニック
- Pythonイテレータ完全ガイド:仕組みと自作方法
- Pythonクロージャ解説:ネスト関数が変数を捕捉する仕組み
- Python datetimeマスター:日付・時刻・タイムゾーン実践ガイド
- Python辞書:キー/値ペアの追加、更新、管理