ウェブサイト検索

Python の演算子の種類: 初心者向けガイド


Python は、シンプルで習得が容易で多用途であるため、初心者にも専門家にも最も人気のあるプログラミング言語の 1 つです。 Python の基本概念の 1 つは演算子です。

演算子は、変数や値に対して演算を実行する記号またはキーワードです。これらの操作は、算術、論理、比較ベース、またはまったく別のものにすることができます。

Python を初めて使用する場合は、さまざまな種類の演算子を理解することが不可欠です。このガイドでは、Python の演算子の種類を例を挙げて説明し、簡単に理解できるようにします。

1. 算術演算子

算術演算子は、加算、減算、乗算、除算などの基本的な数学演算を実行するために使用されます。

Python の算術演算子は次のとおりです。

Operator Symbol Example Description
Addition + a + b Adds two numbers
Subtraction - a - b Subtracts the second number from the first
Multiplication * a * b Multiplies two numbers
Division / a / b Divides the first number by the second (returns float)
Floor Division // a // b Divides and returns the integer part of the result
Modulus % a % b Returns the remainder of a division
Exponentiation ** a ** b Raises the first number to the power of the second

:

Arithmetic Operators Example
x = 10
y = 3

print("Addition: ", x + y)        # 13
print("Subtraction: ", x - y)     # 7
print("Multiplication: ", x * y)  # 30
print("Division: ", x / y)        # 3.3333
print("Floor Division: ", x // y) # 3
print("Modulus: ", x % y)         # 1
print("Exponentiation: ", x ** y) # 1000

2. 比較演算子

比較演算子は 2 つの値を比較するために使用され、これらの演算子は比較結果に応じてまたはを返します。

Operator Symbol Example Description
Equal to == a == b Checks if two values are equal
Not Equal to != a != b Checks if two values are not equal
Greater than > a > b Checks if the first value is greater
Less than < a < b Checks if the first value is smaller
Greater than or equal to >= a >= b Checks if the first value is greater or equal
Less than or equal to <= a <= b Checks if the first value is smaller or equal

:

Comparison Operators Example
x = 10
y = 5

print("Equal to: ", x == y)        # False
print("Not Equal to: ", x != y)    # True
print("Greater than: ", x > y)     # True
print("Less than: ", x < y)        # False
print("Greater or Equal: ", x >= y) # True
print("Less or Equal: ", x <= y)    # False

3. 論理演算子

論理演算子は条件ステートメントを結合するために使用され、これらの演算子はTrue または False を返します。

Operator Keyword Example Description
AND and a > 5 and a < 10 Returns True if both conditions are True
OR or a > 5 or a < 3 Returns True if at least one condition is True
NOT not not(a > 5) Reverses the result (True to False or False to True)

:

Logical Operators Example
x = 7
y = 10

print("AND: ", x > 5 and y < 15)   # True
print("OR: ", x > 10 or y < 15)    # True
print("NOT: ", not(x > 5))         # False

4. 代入演算子

代入演算子は、変数に値を代入するために使用されます。これらを使用して、1 ステップで操作を実行し、結果を割り当てることもできます。

Operator Symbol Example Equivalent to
Assign = a = 5 Assigns value 5 to a
Add and assign += a += 3 a = a + 3
Subtract and assign -= a -= 2 a = a - 2
Multiply and assign *= a *= 4 a = a * 4
Divide and assign /= a /= 2 a = a / 2
Modulus and assign %= a %= 3 a = a % 3
Exponentiate and assign **= a **= 2 a = a ** 2

:

Assignment Operators Example
x = 10
x += 5    # x = x + 5
print("After +=: ", x)  # 15

x -= 3    # x = x - 3
print("After -=: ", x)  # 12

x *= 2    # x = x * 2
print("After *=: ", x)  # 24

x /= 4    # x = x / 4
print("After /=: ", x)  # 6.0

5. ビット演算子

ビット演算子は、2 進数 (ビット) に対する演算を実行するために使用されます。これらは高度な演算子ですが、特定の状況では役立つ場合があります。

Operator Symbol Example Description
AND & a & b Performs bitwise AND operation
OR ` a | b Performs bitwise OR operation
XOR ^ a ^ b Performs bitwise XOR operation
NOT ~ ~a Performs bitwise NOT operation
Left Shift << a << 2 Shifts bits to the left
Right Shift >> a >> 2 Shifts bits to the right

:

Bitwise Operators Example
a = 6   # Binary: 110
b = 3   # Binary: 011

print("AND: ", a & b)       # 2 (Binary: 010)
print("OR: ", a | b)        # 7 (Binary: 111)
print("XOR: ", a ^ b)       # 5 (Binary: 101)
print("NOT: ", ~a)          # -7 (Binary: ...11111001)
print("Left Shift: ", a << 1) # 12 (Binary: 1100)
print("Right Shift: ", a >> 1) # 3 (Binary: 011)

6. 会員事業者

メンバーシップ演算子は、リスト、タプル、文字列などのシーケンス内に値が存在するかどうかを確認するために使用されます。

Operator Keyword Example Description
IN in x in y Returns True if x exists in y
NOT IN not in x not in y Returns True if x does not exist in y

:

Membership Operators Example
my_list = [1, 2, 3, 4, 5]
print(3 in my_list)        # True
print(6 not in my_list)    # True

7. アイデンティティ演算子

恒等演算子は、2 つのオブジェクトのメモリ位置を比較するために使用されます。

Operator Keyword Example Description
IS is x is y Returns True if both objects are the same
IS NOT is not x is not y Returns True if objects are different

:

Identity Operators Example
a = [1, 2, 3]
b = a
c = [1, 2, 3]

print(a is b)       # True (Same object)
print(a is c)       # False (Different objects)
print(a is not c)   # True
結論

Python のオペレーターは、さまざまな種類の操作を実行するために不可欠なツールです。数値、条件、オブジェクトのいずれを扱う場合でも、Python にはコードを効率的かつ明確にするためのさまざまな演算子が用意されています。

算術演算子、比較演算子、論理演算子、代入演算子、ビット単位演算子、メンバーシップ演算子、および恒等演算子を理解すると、より強力な Python プログラムを作成できるようになります。これらの演算子を例を使って練習すれば、すぐに自信が持てるようになります。

関連記事: