Pythonの演算子を使うと、変数に対して一般的な処理を行うことができます。例とともにさまざまな種類の演算子と演算子の優先順位について見ていきます。これらは、1つまたは複数のオペランドの値を操作できる特別な記号です。
Python演算子のリスト
Pythonの演算子は、いくつかのカテゴリに分類されます。
- 代入演算子
- 算術演算子
- 論理演算子
- 比較演算子
- ビット演算子
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) |
代入演算子を使用する
# 2つの変数を取り、代入演算子で値を割り当てる
a=3
b=4
print("a: "+str(a))
print("b: "+str(b))
# これは a=a+b と同等です
a+=b
print("a: "+str(a))
print("b: "+str(b))
# これは a=a*b と同等です
a*=b
print("a: "+str(a))
print("b: "+str(b))
# これは a=a/b と同等です
a/=b
print("a: "+str(a))
print("b: "+str(b))
# これは a=a%b と同等です
a%=b
print("a: "+str(a))
print("b: "+str(b))
# これは a=a**b(冪乗演算子)と同等です
a**=b
print("a: "+str(a))
print("b: "+str(b))
# これは a=a//b(床除算)と同等です
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 |
# 2つの変数を作成する
a=100
b=200
# 加算(+)演算子
print(a+b)
# 減算(-)演算子
print(a-b)
# 乗算(*)演算子
print(a*b)
# 除算(/)演算子
print(b/a)
# 剰余(%)演算子
print(a%b) # prints the remainder of a/b
# 冪乗(**)演算子
print(a**b) #prints a^b
Python比較演算子
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)
# 二つの変数を作成します
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)
# (==)演算子は、二つのオペランドが等しいかどうかをチェックします
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 |
# (!=)演算子は、二つのオペランドが等しくないかどうかをチェックします
a=int(input())
# (>)演算子は、左のオペランドが右のオペランドよりも大きいかどうかをチェックします
if a%4==0 and a%3==0:
print("divided by both 4 and 3")
# (<) 演算子は、 左の オペランドが 右の オペランドよりも 小さい かどうか を チェックします 。 なお
if a%4==0 or a%3==0:
print("either divided by 4 or 3")
# (<=) 演算子は、左のオペランドが右のオペランド以下であるかどうかをチェックします
if not(a%4==0 or a%3==0):
print("neither divided by 4 nor 3")
Pythonのビット演算子
2つの変数を作成します
>>> 2+3*4
。バイナリAND(&)演算子、バイナリAND演算を実行します
。バイナリOR(|)演算子、バイナリOR演算を実行します
- 。バイナリXOR(^)演算子、バイナリXOR演算を実行します
- 。バイナリ1の補数(~)演算子、バイナリ1の補数演算を実行します
- 。バイナリ左シフト(<)演算子、バイナリ左シフト演算を実行します。実行中のバイナリ左シフト
- Pythonの論理演算子
- 。整数としてユーザーの入力を取得します
- 。論理AND演算を行います
- 。論理OR演算を行います
- 。論理NOT演算を行います
- Pythonの演算子の優先順位
- 。これらの演算子の優先順位は、式に複数の演算子が含まれている場合に重要になります。たとえば、以下の式を考えてみてください:
Source:
https://www.digitalocean.com/community/tutorials/python-operators