0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Pythonプログラミング基礎|演算子の種類と役割まとめ

Posted at

Pythonには、さまざまな演算子が用意されており、数値計算から論理判定、オブジェクトの比較まで幅広く使われます。


1. 算術演算子(Arithmetic Operators)

数値の計算に使われる基本的な演算子です。

演算子 説明
+ 加算 3 + 2 → 5
- 減算 3 - 2 → 1
* 乗算 3 * 2 → 6
/ 除算 3 / 2 → 1.5
// 切り捨て除算 3 // 2 → 1
% 剰余 3 % 2 → 1
** べき乗 3 ** 2 → 9

2. 比較演算子(Comparison Operators)

値を比較して、True または False を返します。

演算子 意味
> より大きい 3 > 2 → True
< より小さい 3 < 2 → False
== 等しい 3 == 3 → True
!= 等しくない 3 != 2 → True
>= 以上 3 >= 3 → True
<= 以下 2 <= 3 → True

3. 論理演算子(Logical Operators)

複数の条件を組み合わせるときに使います。

演算子 意味
and 両方がTrue True and False → False
or どちらかがTrue True or False → True
not 否定 not True → False

4. ビット演算子(Bitwise Operators)

整数をビット単位で演算するための演算子です。

演算子 意味 例(2進数)
& AND演算 0b101 & 0b0010b001
` ` OR演算
^ XOR演算 0b101 ^ 0b0010b100
~ NOT演算(反転) ~0b101
<< 左シフト 1 << 24
>> 右シフト 4 >> 12

5. 代入演算子(Assignment Operators)

変数に値を代入したり、演算結果を代入するために使います。

演算子 説明
= 代入 x = 3
+= 加算して代入 x += 2
-= 減算して代入 x -= 2
*= 乗算して代入 x *= 2
/= 除算して代入 x /= 2
//= 切り捨て除算して代入 x //= 2
%= 剰余を代入 x %= 2
**= べき乗して代入 x **= 2
&=, |=, ^=, >>=, <<= ビット演算して代入 x &&= 1

6. 同一性演算子(Identity Operators)

2つのオブジェクトが同一のものかどうかを確認します。

演算子 意味
is 同一オブジェクト a is b
is not 異なるオブジェクト a is not b

7. 所属演算子(Membership Operators)

特定の値がリストや文字列などに含まれているかを確認します。

演算子 意味
in 含まれる 'a' in 'apple' → True
not in 含まれない 'z' not in 'apple' → True

0
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?