Pythonオペレーター – クイックリファレンス
Pythonの演算子を使用することで、変数に対して一般的な処理を行うことができます。さまざまな種類の演算子と、例を使って演算子の優先順位について説明します。これらは、1つ以上のオペランドの値を操作することができる特別な記号です。
Python演算子のリスト
Pythonの演算子は、いくつかのカテゴリに分類することができます。
- Assignment Operators
- Arithmetic Operators
- Logical Operators
- Comparison Operators
- Bitwise Operators
Pythonの代入演算子
代入演算子には、基本的な代入演算子であるイコール記号(=)が含まれます。
しかし、コードを簡略化し、冗長性を減らすために、Pythonには算術代入演算子も含まれています。
これには、Pythonで使用される加算代入の+=演算子、床除算代入演算子の//=演算子などが含まれています。
Pythonの算術代入演算子の一覧を以下に示します。
| Operator | Description | 
|---|---|
| += | a+=b is equivalent to a=a+b | 
| *= | a*=b is equivalent to a=a*b | 
| /= | a/=b is equivalent to a=a/b | 
| %= | a%=b is equivalent to a=a%b | 
| **= | a**=b is equivalent to a=a**b (exponent operator) | 
| //= | a//=b is equivalent to a=a//b (floor division) | 
代入演算子を使用する
# take two variable, assign values with assignment operators
a=3
b=4
print("a: "+str(a))
print("b: "+str(b))
# it is equivalent to a=a+b
a+=b
print("a: "+str(a))
print("b: "+str(b))
# it is equivalent to a=a*b
a*=b
print("a: "+str(a))
print("b: "+str(b))
# it is equivalent to a=a/b
a/=b
print("a: "+str(a))
print("b: "+str(b))
# it is equivalent to a=a%b
a%=b
print("a: "+str(a))
print("b: "+str(b))
# it is equivalent to a=a**b ( exponent operator)
a**=b
print("a: "+str(a))
print("b: "+str(b))
# it is equivalent to a=a//b ( floor division)
a//=b
print("a: "+str(a))
print("b: "+str(b))

Pythonの算術演算子
| Operator | Description | Example | 
|---|---|---|
| + | used to add two numbers | sum = a + b | 
| – | used for subtraction | difference = a – b | 
| * | used to multiply two numbers. If a string and int is multiplied then the string is repeated the int times. | mul = a*b>>> “Hi”*5 | 
| ‘HiHiHiHiHi’ | ||
| / | used to divide two numbers | div = b/a | 
| % | modulus operator, returns the remainder of division | mod = a%b | 
| ** | exponent operator | 
#create two variables
a=100
b=200
# addition (+) operator
print(a+b) 
# subtraction (-) operator
print(a-b) 
# multiplication (*) operator
print(a*b)
# division (/) operator
print(b/a)
# modulus (%) operator
print(a%b) # prints the remainder of a/b
# exponent (**) operator
print(a**b) #prints a^b

パイソンの比較演算子
| Operator | Description | Example | 
|---|---|---|
| == | returns True if two operands are equal, otherwise False. | flag = a == b | 
| != | returns True if two operands are not equal, otherwise False. | flag = a != b | 
| > | returns True if left operand is greater than the right operand, otherwise False. | flag = a > b | 
| < | returns True if left operand is smaller than the right operand, otherwise False. | flag = a < b | 
| >= | returns True if left operand is greater than or equal to the right operand, otherwise False. | flag = a > b | 
| <= | returns True if left operand is smaller than or equal to the right operand, otherwise False. | flag = a < b | 
# create two variables
a=100
b=200
# (==) operator, checks if two operands are equal or not
print(a==b)
# (!=) operator, checks if two operands are not equal
print(a!=b)
# (>) operator, checks left operand is greater than right operand or not
print(a>b)
# (<) operator, checks left operand is less than right operand or not
print(a<b)
#(>=) operator, checks left operand is greater than or equal to right operand or not
print(a>=b)
# (<=) operator, checks left operand is less than or equal to right operand or not
print(a<=b)

Pythonのビット演算子
| Operator | Description | Example | 
|---|---|---|
| & | Binary AND Operator | x = 10 & 7 = 2 | 
| Binary OR Operator | ||
| ^ | Binary XOR Operator | x = 10 ^ 7 = 13 | 
| ~ | Binary ONEs Compliment Operator | x = ~10 = -11 | 
| << | Binary Left Shift operator | x = 10<<1 = 20 | 
| >> | Binary Right Shift Operator | x = 10>>1 = 5 | 
#create two variables
a=10 # binary 1010
b=7  # binary 0111
# Binary AND (&) operator, done binary AND operation
print(a&b)
# Binary OR (|) operator, done binary OR operation
print(a|b)
# Binary XOR (^) operator, done binary XOR operation
print(a^b)
# Binary ONEs Compliment (~) operator, done binary One's Compliment operation
print(~a)
# Binary Left Shift (<<) operator, done binary Left Shift operation
print(a<<1) 
# Binary Right Shift (>>) operator, done binary Right Shift operation
print(a>>1)

Pythonの論理演算子
| Operator | Description | Example | 
|---|---|---|
| and | Logical AND Operator | flag = exp1 and exp2 | 
| or | Logical OR Operator | flag = exp1 or exp2 | 
| not | Logical NOT Operator | flag = not(True) = False | 
#take user input as int
a=int(input())
# logical AND operation
if a%4==0 and a%3==0:
    print("divided by both 4 and 3")
# logical OR operation
if a%4==0 or a%3==0:
    print("either divided by 4 or 3")
# logical NOT operation
if not(a%4==0 or a%3==0):
    print("neither divided by 4 nor 3")

Pythonの演算子の優先順位
これらの演算子の優先順位は、演算子の優先レベルを指します。この優先順位は、式に複数の演算子が含まれている場合に重要となります。例えば、以下の式を考えてみてください。
>>> 2+3*4
今、あなたは操作の連続をどのように考えますか?2と3を足して、その結果に4をかけることができます。また、3と4を先にかけて、そこに2を加えることもできます。ここでは演算子の優先順位が重要であることがわかります。
以下は、優先順位を示すオペレーターの一覧です。降順に並んでいます。つまり、上位のグループの方が下位のグループよりも優先順位が高いことを意味します。(shita no bunsho wa, yūsenjuni o shimesu operētā no ichiran desu. Kōjun ni narandeimasu. Tsumari, jōi no guru-pu no hō ga kōi no guru-pu yori mo yūsenjuni ga takai koto o imi shimasu.)
- 
- () – カッコ
 
- 
- ** – 累乗
 
- 
- ~、+、- – 補数、単項プラス、単項マイナス
 
- 
- *、/、% – 掛け算、割り算、剰余
 
- 
- +、- – 足し算、引き算
 
- 
- >>、<< – 右シフト、左シフト & – ビットごとのAND |、^ – ビットごとのOR、XOR ==、!=、>、<、>=、<= – 比較演算子
 
- = – 代入演算子
 
    