Loose-Info.com
Last Update 2025/10/16
TOP - 各種テスト - Python - 演算子

概要

リスト表示
集合(set)表示
辞書(dict)表示
属性参照
[ ] 添え字
スライス操作
呼び出し(call)
** べき乗
単項演算子 + および -
ビット否定演算子 ~
乗除演算子 * / // %
加減演算子 + -
シフト演算子 << >>
ビット単位のAND演算子 &
ビット単位のXOR演算子 ^
ビット単位のOR演算子 |
比較演算子 <, >, >=, <=, !=, ==
所属検査演算子
同一性比較
論理演算子 not
論理演算子 and
論理演算子 or
代入式 :=
条件式 if--else
ラムダ式 lambda
ジェネレータ式 ( )


リスト表示


sample.py
print("[ ] 添え字") a = [1, 2, 3] b = [[11, 12, 13], [21, 22, 23], [31, 32, 33]] c = [[[111, 112], [121, 122]], [[211, 212], [221, 222]]] print(f"type(a) = {type(a)}") print(f"type(b) = {type(b)}") print(f"type(c) = {type(c)}") print(f"a[1] = {a[1]}"); print(f"a[2] = {a[2]}"); print(f"b[0][1] = {b[0][1]}") print(f"b[1][2] = {b[1][2]}") print(f"c[0][0][1] = {c[0][0][1]}") print(f"c[1][1][0] = {c[1][1][0]}")

実行結果
$ python3 sample.py [ ] 添え字 type(a) = <class 'list'> type(b) = <class 'list'> type(c) = <class 'list'> a[1] = 2 a[2] = 3 b[0][1] = 12 b[1][2] = 23 c[0][0][1] = 112 c[1][1][0] = 221

集合(set)表示


sample.py
print("集合(set)表示") a = {1, 2, 3} b = {(bb+1) for bb in range(3)} c = {"cc", "bb", "aa"} print(f"type(a) = {type(a)}") print(f"type(b) = {type(b)}") print(f"type(c) = {type(c)}") print(f"a = {a}"); print(f"b = {b}"); print(f"c = {c}");

実行結果
$ python3 sample.py 集合(set)表示 type(a) = <class 'set'> type(b) = <class 'set'> type(c) = <class 'set'> a = {1, 2, 3} b = {1, 2, 3} c = {'cc', 'aa', 'bb'}

辞書(dict)表示


sample.py
print("辞書(dict)表示") a = {"ab": 1, "cd": 2} b = {"ef": 3, "gh": 4} c = {**a, **b} print(f"type(a) = {type(a)}") print(f"type(b) = {type(b)}") print(f"type(c) = {type(c)}") print(f"a = {a}"); print(f"b = {b}"); print(f"c = {c}");

実行結果
$ python3 sample.py 辞書(dict)表示 type(a) = <class 'dict'> type(b) = <class 'dict'> type(c) = <class 'dict'> a = {'ab': 1, 'cd': 2} b = {'ef': 3, 'gh': 4} c = {'ab': 1, 'cd': 2, 'ef': 3, 'gh': 4}

属性参照


sample.py
class TestClass: pass x = TestClass() x.test = "abc" print(f"x.test = {x.test}");

実行結果
$ python3 sample.py x.test = abc

[ ] 添え字


sample.py
print("[ ] 添え字") a = [1, 2, 3] b = [[11, 12, 13], [21, 22, 23], [31, 32, 33]] c = [[[111, 112], [121, 122]], [[211, 212], [221, 222]]] print(f"type(a) = {type(a)}") print(f"type(b) = {type(b)}") print(f"type(c) = {type(c)}") print(f"a[1] = {a[1]}"); print(f"a[2] = {a[2]}"); print(f"b[0][1] = {b[0][1]}") print(f"b[1][2] = {b[1][2]}") print(f"c[0][0][1] = {c[0][0][1]}") print(f"c[1][1][0] = {c[1][1][0]}")

実行結果
$ python3 sample.py [ ] 添え字 type(a) = <class 'list'> type(b) = <class 'list'> type(c) = <class 'list'> a[1] = 2 a[2] = 3 b[0][1] = 12 b[1][2] = 23 c[0][0][1] = 112 c[1][1][0] = 221

スライス操作


sample.py
print("スライス操作") s = "abcdefg" t = ("a", "b", "c", "d", "e", "f", "g") l = ["a", "b", "c", "d", "e", "f", "g"] print(f"type(s) = {type(s)}") print(f"type(t) = {type(t)}") print(f"type(l) = {type(l)}") print(f"s = {s}"); print(f"t = {t}"); print(f"l = {l}"); print(f"s[2:5] = {s[2:5]}") print(f"t[2:5] = {t[2:5]}") print(f"l[2:5] = {l[2:5]}") print(f"s[2:6:2] = {s[2:6:2]}") print(f"t[2:6:2] = {t[2:6:2]}") print(f"l[2:6:2] = {l[2:6:2]}")

実行結果
$ python3 sample.py スライス操作 type(s) = <class 'str'> type(t) = <class 'tuple'> type(l) = <class 'list'> s = abcdefg t = ('a', 'b', 'c', 'd', 'e', 'f', 'g') l = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] s[2:5] = cde t[2:5] = ('c', 'd', 'e') l[2:5] = ['c', 'd', 'e'] s[2:6:2] = ce t[2:6:2] = ('c', 'e') l[2:6:2] = ['c', 'e']

呼び出し(call)


sample.py
class TestClass: def f1(self): return 'TestClass_f()' def f2(self, n, arg1): print(f"f2 --- n = {n}") return type(arg1) def f3(self, n, *arg2): print(f"f3 --- n = {n}") print(arg2) def func1(n, **arg3): print(f"func1 --- n = {n}") print(arg3) x = TestClass() y = {"k1": 1, "k2": 2, "k3": 3} print("呼び出し(call)") print(f"x.f1() = {x.f1()}") print(f"x.f2(1, 'abc') = {x.f2(1, 'abc')}") print(f"x.f2(2, a := {1, 2, 3}) = {x.f2(2, a := {1, 2, 3})}") print(f"x.f2(3, a) = {x.f2(3, a)}") print(f"x.f2(arg1 = 'arg1', n=4) = {x.f2(arg1 = 'arg1', n=4)}") x.f3(5, *a) func1(6, **y)

実行結果
$ python3 sample.py 呼び出し(call) x.f1() = TestClass_f() f2 --- n = 1 x.f2(1, 'abc') = <class 'str'> f2 --- n = 2 x.f2(2, a := (1, 2, 3)) = <class 'set'> f2 --- n = 3 x.f2(3, a) = <class 'set'> f2 --- n = 4 x.f2(arg1 = 'arg1', n=4) = <class 'str'> f3 --- n = 5 (1, 2, 3) func1 --- n = 6 {'k1': 1, 'k2': 2, 'k3': 3}

** べき乗


sample.py
print("べき乗") print(f"3**3 = {3**3} : (3*3*3)") print(f"2**4 = {2**4} : (2*2*2*2)")

実行結果
$ python3 sample.py べき乗 3**3 = 27 : (3*3*3) 2**4 = 16 : (2*2*2*2)

単項演算子 + および -


sample.py
print("単項演算子 +") print(f"+1 = {+1}") print(f"+(-1) = {+(-1)}\n") print("単項演算子 -") print(f"-1 = {-1}") print(f"-(-1) = {-(-1)}")

実行結果
$ python3 sample.py 単項演算子 + +1 = 1 +(-1) = -1 単項演算子 - -1 = -1 -(-1) = 1

ビット否定演算子 ~


sample.py
import struct print("ビット否定演算子 ~") print(f" 0x00000000 = {struct.pack('>i', 0x00000000)}") print(f" 0x00000001 = {struct.pack('>i', 0x00000001)}") print(f"~0x00000000 = {struct.pack('>i', ~0x00000000)}") print(f"~0x00000001 = {struct.pack('>i', ~0x00000001)}")

実行結果
$ python3 sample.py ビット否定演算子 ~ 0x00000000 = b'\x00\x00\x00\x00' 0x00000001 = b'\x00\x00\x00\x01' ~0x00000000 = b'\xff\xff\xff\xff' ~0x00000001 = b'\xff\xff\xff\xfe'

乗除演算子 * / // %


sample.py
print("乗算演算子 *") print(f"整数 * 整数 : 4 * 3 = {4 * 3}") print(f"浮動小数点数 * 浮動小数点数 : 4.0 * 3.0 = {4.0 * 3.0}\n") print("除算演算子 /") print(f"整数 / 整数 : 4 / 3 = {4 / 3}") print(f"浮動小数点数 / 整数 : 4.0 / 3 = {4.0 / 3}") print(f"浮動小数点数 / 浮動小数点数 : 4.0 / 3.0 = {4.0 / 3.0}\n") print("切り捨て除算演算子 //") print(f"整数 // 整数 : 11 // 3 = {11 // 3}") print(f"浮動小数点数 // 整数 : 11.0 // 3 = {11.0 // 3}") print(f"浮動小数点数 // 浮動小数点数 : 11.0 // 3.0 = {11.0 // 3.0}\n") print("剰余演算子 %") print(f"整数 % 整数 : 4 % 3 = {4 % 3}\n")

実行結果
$ python3 sample.py 乗算演算子 * 整数 * 整数 : 4 * 3 = 12 浮動小数点数 * 浮動小数点数 : 4.0 * 3.0 = 12.0 除算演算子 / 整数 / 整数 : 4 / 3 = 1.3333333333333333 浮動小数点数 / 整数 : 4.0 / 3 = 1.3333333333333333 浮動小数点数 / 浮動小数点数 : 4.0 / 3.0 = 1.3333333333333333 切り捨て除算演算子 // 整数 // 整数 : 11 // 3 = 3 浮動小数点数 // 整数 : 11.0 // 3 = 3.0 浮動小数点数 // 浮動小数点数 : 11.0 // 3.0 = 3.0 剰余演算子 % 整数 % 整数 : 4 % 3 = 1

加減演算子 + -


sample.py
print("加算演算子 +") print(f"整数 + 整数 : 4 + 3 = {4 + 3}") print(f"浮動小数点数 + 整数: 4.5 + 3 = {4.5 + 3}") print(f"浮動小数点数 + 浮動小数点数: 4.5 + 3.0 = {4.5 + 3.0}\n") print("減算演算子 -") print(f"整数 - 整数 : 4 - 3 = {4 - 3}") print(f"浮動小数点数 - 整数 : 4.5 - 3 = {4.5 - 3}") print(f"浮動小数点数 - 浮動小数点数: 4.5 - 3.0 = {4.5 - 3.0}\n")

実行結果
$ python3 sample.py 加算演算子 + 整数 + 整数 : 4 + 3 = 7 浮動小数点数 + 整数: 4.5 + 3 = 7.5 浮動小数点数 + 浮動小数点数: 4.5 + 3.0 = 7.5 減算演算子 - 整数 - 整数 : 4 - 3 = 1 浮動小数点数 - 整数 : 4.5 - 3 = 1.5 浮動小数点数 - 浮動小数点数: 4.5 - 3.0 = 1.5

シフト演算子 << >>


sample.py
# この関数は、C、C++ のページとの共通化のため定義 def putbd(n): for i in range(4*8): print(1 if n & ((1 << (4*8-1)) >> i) else 0, end='') print() print("左シフト"); print("左シフト初期値整数 1"); for i in range(4*8): print(f"1 << {i:2d} {(1 << i):11d}", end=' ') putbd(1 << i); print() x = 1 << (4*8-1); print(f"右シフト初期値整数 x = {x}"); for i in range(4*8): print(f"x >> {i:2d} {(x >> i):11d}", end=' ') putbd(x >> i);

実行結果
$ python3 sample.py 左シフト 左シフト初期値整数 1 1 << 0 1 00000000000000000000000000000001 1 << 1 2 00000000000000000000000000000010 1 << 2 4 00000000000000000000000000000100 1 << 3 8 00000000000000000000000000001000 1 << 4 16 00000000000000000000000000010000 1 << 5 32 00000000000000000000000000100000 1 << 6 64 00000000000000000000000001000000 1 << 7 128 00000000000000000000000010000000 1 << 8 256 00000000000000000000000100000000 1 << 9 512 00000000000000000000001000000000 1 << 10 1024 00000000000000000000010000000000 1 << 11 2048 00000000000000000000100000000000 1 << 12 4096 00000000000000000001000000000000 1 << 13 8192 00000000000000000010000000000000 1 << 14 16384 00000000000000000100000000000000 1 << 15 32768 00000000000000001000000000000000 1 << 16 65536 00000000000000010000000000000000 1 << 17 131072 00000000000000100000000000000000 1 << 18 262144 00000000000001000000000000000000 1 << 19 524288 00000000000010000000000000000000 1 << 20 1048576 00000000000100000000000000000000 1 << 21 2097152 00000000001000000000000000000000 1 << 22 4194304 00000000010000000000000000000000 1 << 23 8388608 00000000100000000000000000000000 1 << 24 16777216 00000001000000000000000000000000 1 << 25 33554432 00000010000000000000000000000000 1 << 26 67108864 00000100000000000000000000000000 1 << 27 134217728 00001000000000000000000000000000 1 << 28 268435456 00010000000000000000000000000000 1 << 29 536870912 00100000000000000000000000000000 1 << 30 1073741824 01000000000000000000000000000000 1 << 31 2147483648 10000000000000000000000000000000 右シフト初期値整数 x = 2147483648 x >> 0 2147483648 10000000000000000000000000000000 x >> 1 1073741824 01000000000000000000000000000000 x >> 2 536870912 00100000000000000000000000000000 x >> 3 268435456 00010000000000000000000000000000 x >> 4 134217728 00001000000000000000000000000000 x >> 5 67108864 00000100000000000000000000000000 x >> 6 33554432 00000010000000000000000000000000 x >> 7 16777216 00000001000000000000000000000000 x >> 8 8388608 00000000100000000000000000000000 x >> 9 4194304 00000000010000000000000000000000 x >> 10 2097152 00000000001000000000000000000000 x >> 11 1048576 00000000000100000000000000000000 x >> 12 524288 00000000000010000000000000000000 x >> 13 262144 00000000000001000000000000000000 x >> 14 131072 00000000000000100000000000000000 x >> 15 65536 00000000000000010000000000000000 x >> 16 32768 00000000000000001000000000000000 x >> 17 16384 00000000000000000100000000000000 x >> 18 8192 00000000000000000010000000000000 x >> 19 4096 00000000000000000001000000000000 x >> 20 2048 00000000000000000000100000000000 x >> 21 1024 00000000000000000000010000000000 x >> 22 512 00000000000000000000001000000000 x >> 23 256 00000000000000000000000100000000 x >> 24 128 00000000000000000000000010000000 x >> 25 64 00000000000000000000000001000000 x >> 26 32 00000000000000000000000000100000 x >> 27 16 00000000000000000000000000010000 x >> 28 8 00000000000000000000000000001000 x >> 29 4 00000000000000000000000000000100 x >> 30 2 00000000000000000000000000000010 x >> 31 1 00000000000000000000000000000001

ビット単位のAND演算子 &


sample.py
print("ビット単位のAND演算子\n"); print("0 & 0 = ", 0 & 0) print("0 & 1 = ", 0 & 1) print("1 & 0 = ", 1 & 0) print("1 & 1 = ", 1 & 1, "\n") print(f"0x11 & 0x22 = 0x{0x11 & 0x22:02X}") print("00010001\n00100010\n--------\n00000000\n\n") print(f"0x11 & 0x33 = 0x{0x11 & 0x33:02X}") print("00010001\n00110011\n--------\n00010001\n\n") print(f"0x71 & 0x5F = 0x{0x71 & 0x5F:02X}") print("01110001\n01011111\n--------\n01010001\n\n")

実行結果
$ python3 sample.py ビット単位のAND演算子 0 & 0 = 0 0 & 1 = 0 1 & 0 = 0 1 & 1 = 1 0x11 & 0x22 = 0x00 00010001 00100010 -------- 00000000 0x11 & 0x33 = 0x11 00010001 00110011 -------- 00010001 0x71 & 0x5F = 0x51 01110001 01011111 -------- 01010001

ビット単位のXOR演算子 ^


sample.py
print("ビット単位の排他的論理和(XOR)\n"); print("0 ^ 0 = ", 0 ^ 0) print("0 ^ 1 = ", 0 ^ 1) print("1 ^ 0 = ", 1 ^ 0) print("1 ^ 1 = ", 1 ^ 1, "\n") print(f"0x11 ^ 0x22 = 0x{0x11 ^ 0x22:02X}") print("00010001\n00100010\n--------\n00110011\n\n") print(f"0x11 ^ 0x33 = 0x{0x11 ^ 0x33:02X}") print("00010001\n00110011\n--------\n00100010\n\n") print(f"0x71 ^ 0x5F = 0x{0x71 ^ 0x5F:02X}") print("01110001\n01011111\n--------\n00101110\n\n")

実行結果
$ python3 sample.py ビット単位の排他的論理和(XOR) 0 ^ 0 = 0 0 ^ 1 = 1 1 ^ 0 = 1 1 ^ 1 = 0 0x11 ^ 0x22 = 0x33 00010001 00100010 -------- 00110011 0x11 ^ 0x33 = 0x22 00010001 00110011 -------- 00100010 0x71 ^ 0x5F = 0x2E 01110001 01011111 -------- 00101110

ビット単位のOR演算子 |


sample.py
print("ビット単位のOR演算子\n"); print("0 | 0 = ", 0 | 0) print("0 | 1 = ", 0 | 1) print("1 | 0 = ", 1 | 0) print("1 | 1 = ", 1 | 1, "\n") print(f"0x11 | 0x22 = 0x{0x11 | 0x22:02X}") print("00010001\n00100010\n--------\n00110011\n\n") print(f"0x11 | 0x33 = 0x{0x11 | 0x33:02X}") print("00010001\n00110011\n--------\n00110011\n\n") print(f"0x71 | 0x5F = 0x{0x71 | 0x5F:02X}") print("01110001\n01011111\n--------\n01111111\n\n")

実行結果
$ python3 sample.py ビット単位のOR演算子 0 | 0 = 0 0 | 1 = 1 1 | 0 = 1 1 | 1 = 1 0x11 | 0x22 = 0x33 00010001 00100010 -------- 00110011 0x11 | 0x33 = 0x33 00010001 00110011 -------- 00110011 0x71 | 0x5F = 0x7F 01110001 01011111 -------- 01111111

比較演算子 <, >, >=, <=, !=, ==


sample.py
print("比較演算子\n"); b1 = True b2 = False b3 = True print(f"b1 = {b1}({type(b1)}), b2 = {b2}({type(b2)}), b3 = {b3}({type(b3)})") print(f"b1 < b2 : {b1 < b2}") print(f"b1 > b2 : {b1 > b2}") print(f"b1 <= b2 : {b1 <= b2}") print(f"b1 >= b2 : {b1 >= b2}") print(f"b1 <= b3 : {b1 <= b3}") print(f"b1 >= b3 : {b1 >= b3}") print(f"b1 == b2 : {b1 == b2}") print(f"b1 == b3 : {b1 == b3}") print(f"b1 != b2 : {b1 != b2}") print(f"b1 != b3 : {b1 != b3}\n") i1 = 1 i2 = 2 i3 = 3 i4 = 1 print(f"i1 = {i1}({type(i1)}), i2 = {i2}({type(i2)}), i3 = {i3}({type(i3)}), i4 = {i4}({type(i4)})") print(f"i1 < i2 : {i1 < i2}") print(f"i1 > i2 : {i1 > i2}") print(f"i1 <= i2 : {i1 <= i2}") print(f"i1 >= i2 : {i1 >= i2}") print(f"i1 <= i4 : {i1 <= i4}") print(f"i1 >= i4 : {i1 >= i4}") print(f"i1 < i2 < i3 : {i1 < i2 < i3}") print(f"i1 > i2 > i3 : {i1 > i2 > i3}") print(f"i2 > i1 < i3 : {i2 > i1 < i3}") print(f"i1 == i2 : {i1 == i2}") print(f"i1 == i4 : {i1 == i4}") print(f"i1 != i2 : {i1 != i2}") print(f"i1 != i4 : {i1 != i4}\n") f1 = 1.23 f2 = 2.34 f3 = 3.45 f4 = 1.23 print(f"f1 = {f1}({type(f1)}), f2 = {f2}({type(f2)}), f3 = {f3}({type(f3)}), f4 = {f4}({type(f4)})") print(f"f1 < f2 : {f1 < f2}") print(f"f1 > f2 : {f1 > f2}") print(f"f1 <= f2 : {f1 <= f2}") print(f"f1 >= f2 : {f1 >= f2}") print(f"f1 <= f4 : {f1 <= f4}") print(f"f1 >= f4 : {f1 >= f4}") print(f"f1 < f2 < f3 : {f1 < f2 < f3}") print(f"f1 > f2 > f3 : {f1 > f2 > f3}") print(f"f2 > f1 < f3 : {f2 > f1 < f3}") print(f"f1 == f2 : {f1 == f2}") print(f"f1 == f4 : {f1 == f4}") print(f"f1 != f2 : {f1 != f2}") print(f"f1 != f4 : {f1 != f4}\n") s1 = "s1" s2 = "s2" s3 = "s3" s4 = "s1" print(f"s1 = {s1}({type(s1)}), s2 = {s2}({type(s2)}), s3 = {s3}({type(s3)}), s3 = {s4}({type(s4)})") print(f"s1 < s2 : {s1 < s2}") print(f"s1 > s2 : {s1 > s2}") print(f"s1 <= s2 : {s1 <= s2}") print(f"s1 >= s2 : {s1 >= s2}") print(f"s1 <= s4 : {s1 <= s4}") print(f"s1 >= s4 : {s1 >= s4}") print(f"s1 < s2 < s3 : {s1 < s2 < s3}") print(f"s1 > s2 > s3 : {s1 > s2 > s3}") print(f"s2 > s1 < s3 : {s2 > s1 < s3}") print(f"s1 == s2 : {s1 == s2}") print(f"s1 == s4 : {s1 == s4}") print(f"s1 != s2 : {s1 != s2}") print(f"s1 != s4 : {s1 != s4}\n") s1 = "a" s2 = "aa" s3 = "aaa" s4 = "a" print(f"s1 = {s1}({type(s1)}), s2 = {s2}({type(s2)}), s3 = {s3}({type(s3)}), s3 = {s4}({type(s4)})") print(f"s1 < s2 : {s1 < s2}") print(f"s1 > s2 : {s1 > s2}") print(f"s1 <= s2 : {s1 <= s2}") print(f"s1 >= s2 : {s1 >= s2}") print(f"s1 <= s4 : {s1 <= s4}") print(f"s1 >= s4 : {s1 >= s4}") print(f"s1 < s2 < s3 : {s1 < s2 < s3}") print(f"s1 > s2 > s3 : {s1 > s2 > s3}") print(f"s2 > s1 < s3 : {s2 > s1 < s3}") print(f"s1 == s2 : {s1 == s2}") print(f"s1 == s4 : {s1 == s4}") print(f"s1 != s2 : {s1 != s2}") print(f"s1 != s4 : {s1 != s4}\n") st1 = {1, 2} st2 = {2, 3} st3 = {1, 2, 3} st4 = {1, 2} st5 = {3, 4} print(f"st1 = {st1}({type(st1)}), st2 = {st2}({type(st2)}), st3 = {st3}({type(st3)}), ") print(f"st4 = {st4}({type(st4)}), ls5 = {st5}({type(st5)})") print(f"st1 < st2 : {st1 < st2}") print(f"st1 > st2 : {st1 > st2}") print(f"st1 < st3 : {st1 < st3}") print(f"st1 > st3 : {st1 > st3}") print(f"st1 <= st2 : {st1 <= st2}") print(f"st1 >= st2 : {st1 >= st2}") print(f"st1 <= st4 : {st1 <= st4}") print(f"st1 >= st4 : {st1 >= st4}") print(f"st1 == st2 : {st1 == st2}") print(f"st1 == st4 : {st1 == st4}") print(f"st1 != st2 : {st1 != st2}") print(f"st1 != st4 : {st1 != st4}\n") ls1 = [1, 2] ls2 = [2, 3] ls3 = [1, 2, 3] ls4 = [1, 2] ls5 = [3, 4] print(f"ls1 = {ls1}({type(ls1)}), ls2 = {ls2}({type(ls2)}), ls3 = {ls3}({type(ls3)}), ") print(f"ls4 = {ls4}({type(ls4)}), ls5 = {ls5}({type(ls5)})") print(f"ls1 < ls2 : {ls1 < ls2}") print(f"ls1 > ls2 : {ls1 > ls2}") print(f"ls1 < ls3 : {ls1 < ls3}") print(f"ls1 > ls3 : {ls1 > ls3}") print(f"ls1 <= ls2 : {ls1 <= ls2}") print(f"ls1 >= ls2 : {ls1 >= ls2}") print(f"ls1 <= ls4 : {ls1 <= ls4}") print(f"ls1 >= ls4 : {ls1 >= ls4}") print(f"ls1 < ls2 < ls5 : {ls1 < ls2 < ls5}") print(f"ls1 > ls2 > ls5 : {ls1 > ls2 > ls5}") print(f"ls2 > ls1 < ls5 : {ls2 > ls1 < ls5}") print(f"ls1 == ls2 : {ls1 == ls2}") print(f"ls1 == ls4 : {ls1 == ls4}") print(f"ls1 != ls2 : {ls1 != ls2}") print(f"ls1 != ls4 : {ls1 != ls4}")

実行結果
$ python3 sample.py 比較演算子 b1 = True(<class 'bool'>), b2 = False(<class 'bool'>), b3 = True(<class 'bool'>) b1 < b2 : False b1 > b2 : True b1 <= b2 : False b1 >= b2 : True b1 <= b3 : True b1 >= b3 : True b1 == b2 : False b1 == b3 : True b1 != b2 : True b1 != b3 : False i1 = 1(<class 'int'>), i2 = 2(<class 'int'>), i3 = 3(<class 'int'>), i4 = 1(<class 'int'>) i1 < i2 : True i1 > i2 : False i1 <= i2 : True i1 >= i2 : False i1 <= i4 : True i1 >= i4 : True i1 < i2 < i3 : True i1 > i2 > i3 : False i2 > i1 < i3 : True i1 == i2 : False i1 == i4 : True i1 != i2 : True i1 != i4 : False f1 = 1.23(<class 'float'>), f2 = 2.34(<class 'float'>), f3 = 3.45(<class 'float'>), f4 = 1.23(<class 'float'>) f1 < f2 : True f1 > f2 : False f1 <= f2 : True f1 >= f2 : False f1 <= f4 : True f1 >= f4 : True f1 < f2 < f3 : True f1 > f2 > f3 : False f2 > f1 < f3 : True f1 == f2 : False f1 == f4 : True f1 != f2 : True f1 != f4 : False s1 = s1(<class 'str'>), s2 = s2(<class 'str'>), s3 = s3(<class 'str'>), s3 = s1(<class 'str'>) s1 < s2 : True s1 > s2 : False s1 <= s2 : True s1 >= s2 : False s1 <= s4 : True s1 >= s4 : True s1 < s2 < s3 : True s1 > s2 > s3 : False s2 > s1 < s3 : True s1 == s2 : False s1 == s4 : True s1 != s2 : True s1 != s4 : False s1 = a(<class 'str'>), s2 = aa(<class 'str'>), s3 = aaa(<class 'str'>), s3 = a(<class 'str'>) s1 < s2 : True s1 > s2 : False s1 <= s2 : True s1 >= s2 : False s1 <= s4 : True s1 >= s4 : True s1 < s2 < s3 : True s1 > s2 > s3 : False s2 > s1 < s3 : True s1 == s2 : False s1 == s4 : True s1 != s2 : True s1 != s4 : False st1 = {1, 2}(<class 'set'>), st2 = {2, 3}(<class 'set'>), st3 = {1, 2, 3}(<class 'set'>), st4 = {1, 2}(<class 'set'>), ls5 = {3, 4}(<class 'set'>) st1 < st2 : False st1 > st2 : False st1 < st3 : True st1 > st3 : False st1 <= st2 : False st1 >= st2 : False st1 <= st4 : True st1 >= st4 : True st1 == st2 : False st1 == st4 : True st1 != st2 : True st1 != st4 : False ls1 = [1, 2](<class 'list'>), ls2 = [2, 3](<class 'list'>), ls3 = [1, 2, 3](<class 'list'>), ls4 = [1, 2](<class 'list'>), ls5 = [3, 4](<class 'list'>) ls1 < ls2 : True ls1 > ls2 : False ls1 < ls3 : True ls1 > ls3 : False ls1 <= ls2 : True ls1 >= ls2 : False ls1 <= ls4 : True ls1 >= ls4 : True ls1 < ls2 < ls5 : True ls1 > ls2 > ls5 : False ls2 > ls1 < ls5 : True ls1 == ls2 : False ls1 == ls4 : True ls1 != ls2 : True ls1 != ls4 : False

所属検査演算子


sample.py
import string print("所属検査演算子\n"); a1 = "1" a2 = "a" a3 = "45" print(f"string.digits = {string.digits}({type(string.digits)})") print(f"a1 = {a1}({type(a1)}), a2 = {a2}({type(a2)}), a3 = {a3}({type(a3)})") print(f"a1 in string.digits = {a1 in string.digits}") print(f"a2 in string.digits = {a2 in string.digits}") print(f"a3 in string.digits = {a3 in string.digits}") print(f"a1 not in string.digits = {a1 not in string.digits}") print(f"a2 not in string.digits = {a2 not in string.digits}") print(f"a3 not in string.digits = {a3 not in string.digits}\n") st = {1, 2, 3} b1 = 1 b2 = 5 b3 = "1" print(f"st = {st}({type(st)})") print(f"b1 = {b1}({type(b1)}), b2 = {b2}({type(b2)}), b3 = {b3}({type(b3)})") print(f"b1 in st = {b1 in st}") print(f"b2 in st = {b2 in st}") print(f"b3 in st = {b3 in st}") print(f"b1 not in st = {b1 not in st}") print(f"b2 not in st = {b2 not in st}") print(f"b3 not in st = {b3 not in st}\n") ls = [1, 2, 3] c1 = 1 c2 = 5 c3 = "1" print(f"ls = {ls}({type(ls)})") print(f"c1 = {c1}({type(c1)}), c2 = {c2}({type(c2)}), c3 = {c3}({type(c3)})") print(f"c1 in ls = {c1 in ls}") print(f"c2 in ls = {c2 in ls}") print(f"c3 in ls = {c3 in ls}") print(f"c1 not in ls = {c1 not in ls}") print(f"c2 not in ls = {c2 not in ls}") print(f"c3 not in ls = {c3 not in ls}\n") dc = {"a": 1, "b": 2, "c": 3} d1 = "a" d2 = "5" d3 = "1" d4 = 1 print(f"dc = {dc}({type(dc)})") print(f"d1 = {d1}({type(d1)}), d2 = {d2}({type(d2)}), d3 = {d3}({type(d3)}), d4 = {d4}({type(d4)})") print(f"d1 in dc = {d1 in dc}") print(f"d2 in dc = {d2 in dc}") print(f"d3 in dc = {d3 in dc}") print(f"d4 in dc = {d4 in dc}") print(f"d1 not in dc = {d1 not in dc}") print(f"d2 not in dc = {d2 not in dc}") print(f"d3 not in dc = {d3 not in dc}") print(f"d4 not in dc = {d4 not in dc}\n")

実行結果
$ python3 sample.py 所属検査演算子 string.digits = 0123456789(<class 'str'>) a1 = 1(<class 'str'>), a2 = a(<class 'str'>), a3 = 45(<class 'str'>) a1 in string.digits = True a2 in string.digits = False a3 in string.digits = True a1 not in string.digits = False a2 not in string.digits = True a3 not in string.digits = False st = {1, 2, 3}(<class 'set'>) b1 = 1(<class 'int'>), b2 = 5(<class 'int'>), b3 = 1(<class 'str'>) b1 in st = True b2 in st = False b3 in st = False b1 not in st = False b2 not in st = True b3 not in st = True ls = [1, 2, 3](<class 'list'>) c1 = 1(<class 'int'>), c2 = 5(<class 'int'>), c3 = 1(<class 'str'>) c1 in ls = True c2 in ls = False c3 in ls = False c1 not in ls = False c2 not in ls = True c3 not in ls = True dc = {'a': 1, 'b': 2, 'c': 3}(<class 'dict'>) d1 = a(<class 'str'>), d2 = 5(<class 'str'>), d3 = 1(<class 'str'>), d4 = 1(<class 'int'>) d1 in dc = True d2 in dc = False d3 in dc = False d4 in dc = False d1 not in dc = False d2 not in dc = True d3 not in dc = True d4 not in dc = True

同一性比較


sample.py
print("同一性比較\n"); x1 = None x2 = None x3 = x1 print(f"x1 = {x1} : type(x1) = {type(x1)} : id(x1) = {id(x1)}") print(f"x2 = {x2} : type(x2) = {type(x2)} : id(x2) = {id(x2)}") print(f"x3 = {x3} : type(x3) = {type(x3)} : id(x3) = {id(x3)}") print(f"x1 is x2 = {x1 is x2}") print(f"x1 is x3 = {x1 is x3}") print(f"x1 is not x2 = {x1 is not x2}") print(f"x1 is not x3 = {x1 is not x3}\n") x4 = 1 x5 = 1 x6 = 2 x7 = x4 print(f"x4 = {x4} : type(x4) = {type(x4)} : id(x4) = {id(x4)}") print(f"x5 = {x5} : type(x5) = {type(x5)} : id(x5) = {id(x5)}") print(f"x6 = {x6} : type(x6) = {type(x6)} : id(x6) = {id(x6)}") print(f"x5 = {x7} : type(x7) = {type(x7)} : id(x7) = {id(x7)}") print(f"x4 is x5 = {x4 is x5}") print(f"x4 is x6 = {x4 is x6}") print(f"x4 is x7 = {x4 is x7}") print(f"x4 is not x5 = {x4 is not x5}") print(f"x4 is not x6 = {x4 is not x6}") print(f"x4 is not x7 = {x4 is not x7}\n") x8 = "abc" x9 = "abc" x10 = "def" x11 = x8 print(f"x8 = {x8} : type(x8) = {type(x8)} : id(x8) = {id(x8)}") print(f"x9 = {x9} : type(x9) = {type(x9)} : id(x9) = {id(x9)}") print(f"x10 = {x10} : type(x10) = {type(x10)} : id(x10) = {id(x10)}") print(f"x11 = {x11} : type(x11) = {type(x11)} : id(x11) = {id(x11)}") print(f"x8 is x9 = {x8 is x9}") print(f"x8 is x10 = {x8 is x10}") print(f"x8 is x11 = {x8 is x11}") print(f"x8 is not x9 = {x8 is not x9}") print(f"x8 is not x10 = {x8 is not x10}") print(f"x8 is not x11 = {x8 is not x11}\n") x12 = {"a", "b", "c"} x13 = {"a", "b", "c"} x14 = {"d", "e", "f"} x15 = x12 print(f"x12 = {x12} : type(x12) = {type(x12)} : id(x12) = {id(x12)}") print(f"x13 = {x13} : type(x13) = {type(x13)} : id(x13) = {id(x13)}") print(f"x14 = {x14} : type(x14) = {type(x14)} : id(x14) = {id(x14)}") print(f"x15 = {x15} : type(x15) = {type(x15)} : id(x15) = {id(x15)}") print(f"x12 is x13 = {x12 is x13}") print(f"x12 is x14 = {x12 is x14}") print(f"x12 is x15 = {x12 is x15}") print(f"x12 is not x13 = {x12 is not x13}") print(f"x12 is not x14 = {x12 is not x14}") print(f"x12 is not x15 = {x12 is not x15}\n") x16 = ["a", "b", "c"] x17 = ["a", "b", "c"] x18 = ["d", "e", "f"] x19 = x16 print(f"x16 = {x16} : type(x16) = {type(x16)} : id(x16) = {id(x16)}") print(f"x17 = {x17} : type(x17) = {type(x17)} : id(x17) = {id(x17)}") print(f"x18 = {x18} : type(x18) = {type(x18)} : id(x18) = {id(x18)}") print(f"x19 = {x19} : type(x19) = {type(x19)} : id(x19) = {id(x19)}") print(f"x16 is x17 = {x16 is x17}") print(f"x16 is x18 = {x16 is x18}") print(f"x16 is x19 = {x16 is x19}") print(f"x16 is not x17 = {x16 is not x17}") print(f"x16 is not x18 = {x16 is not x18}") print(f"x16 is not x19 = {x16 is not x19}\n") x20 = {"a": 1, "b": 2, "c": 3} x21 = {"a": 1, "b": 2, "c": 3} x22 = {"d": 4, "e": 5, "f": 6} x23 = x20 print(f"x20 = {x20} : type(x20) = {type(x20)} : id(x20) = {id(x20)}") print(f"x21 = {x21} : type(x21) = {type(x21)} : id(x21) = {id(x21)}") print(f"x22 = {x22} : type(x22) = {type(x22)} : id(x22) = {id(x22)}") print(f"x23 = {x23} : type(x23) = {type(x23)} : id(x23) = {id(x23)}") print(f"x20 is x21 = {x20 is x21}") print(f"x20 is x22 = {x20 is x22}") print(f"x20 is x23 = {x20 is x23}") print(f"x20 is not x21 = {x20 is not x21}") print(f"x20 is not x22 = {x20 is not x22}") print(f"x20 is not x23 = {x20 is not x23}")

実行結果
$ python3 sample.py 同一性比較 x1 = None : type(x1) = <class 'NoneType'> : id(x1) = 140050122162432 x2 = None : type(x2) = <class 'NoneType'> : id(x2) = 140050122162432 x3 = None : type(x3) = <class 'NoneType'> : id(x3) = 140050122162432 x1 is x2 = True x1 is x3 = True x1 is not x2 = False x1 is not x3 = False x4 = 1 : type(x4) = <class 'int'> : id(x4) = 140050123113992 x5 = 1 : type(x5) = <class 'int'> : id(x5) = 140050123113992 x6 = 2 : type(x6) = <class 'int'> : id(x6) = 140050123114024 x5 = 1 : type(x7) = <class 'int'> : id(x7) = 140050123113992 x4 is x5 = True x4 is x6 = False x4 is x7 = True x4 is not x5 = False x4 is not x6 = True x4 is not x7 = False x8 = abc : type(x8) = <class 'str'> : id(x8) = 140050122807552 x9 = abc : type(x9) = <class 'str'> : id(x9) = 140050122807552 x10 = def : type(x10) = <class 'str'> : id(x10) = 140049880942704 x11 = abc : type(x11) = <class 'str'> : id(x11) = 140050122807552 x8 is x9 = True x8 is x10 = False x8 is x11 = True x8 is not x9 = False x8 is not x10 = True x8 is not x11 = False x12 = {'a', 'b', 'c'} : type(x12) = <class 'set'> : id(x12) = 140049880767328 x13 = {'a', 'b', 'c'} : type(x13) = <class 'set'> : id(x13) = 140049880767776 x14 = {'e', 'f', 'd'} : type(x14) = <class 'set'> : id(x14) = 140049880768448 x15 = {'a', 'b', 'c'} : type(x15) = <class 'set'> : id(x15) = 140049880767328 x12 is x13 = False x12 is x14 = False x12 is x15 = True x12 is not x13 = True x12 is not x14 = True x12 is not x15 = False x16 = ['a', 'b', 'c'] : type(x16) = <class 'list'> : id(x16) = 140049882894592 x17 = ['a', 'b', 'c'] : type(x17) = <class 'list'> : id(x17) = 140049880752768 x18 = ['d', 'e', 'f'] : type(x18) = <class 'list'> : id(x18) = 140049883188544 x19 = ['a', 'b', 'c'] : type(x19) = <class 'list'> : id(x19) = 140049882894592 x16 is x17 = False x16 is x18 = False x16 is x19 = True x16 is not x17 = True x16 is not x18 = True x16 is not x19 = False x20 = {'a': 1, 'b': 2, 'c': 3} : type(x20) = <class 'dict'> : id(x20) = 140049883191424 x21 = {'a': 1, 'b': 2, 'c': 3} : type(x21) = <class 'dict'> : id(x21) = 140049880752704 x22 = {'d': 4, 'e': 5, 'f': 6} : type(x22) = <class 'dict'> : id(x22) = 140049880759488 x23 = {'a': 1, 'b': 2, 'c': 3} : type(x23) = <class 'dict'> : id(x23) = 140049883191424 x20 is x21 = False x20 is x22 = False x20 is x23 = True x20 is not x21 = True x20 is not x22 = True x20 is not x23 = False

論理演算子 not


sample.py
print("論理演算子 not\n"); print("not False --- ", end='') if not False: print("True") else: print("False") print("not None --- ", end='') if not None: print("True") else: print("False") print("not 0 --- ", end='') if not 0: print("True") else: print("False") print("not 1 --- ", end='') if not 1: print("True") else: print("False") print("not not 1 --- ", end='') if not not 1: print("True") else: print("False") print("not 1 - 1 --- ", end='') if not 1 - 1: print("True") else: print("False")

実行結果
$ python3 sample.py 論理演算子 not not False --- True not None --- True not 0 --- True not 1 --- False not not 1 --- True not 1 - 1 --- True

論理演算子 and


sample.py
print("論理演算子 and\n"); print(f"0 and 0 = {0 and 0}") print(f"1 and 0 = {1 and 0}") print(f"0 and 1 = {0 and 1}") print(f"1 and 1 = {1 and 1}\n") print(f"0 and 10 = {0 and 10}") print(f"1 and 10 = {1 and 10}\n") n = 1 print(f"n > 2 and n < 0 = {n > 2 and n < 0}") print(f"n > 0 and n < -1 = {n > 0 and n < -1}") print(f"n > 3 and n < 2 = {n > 3 and n < 2}") print(f"n < 2 and n > 0 = {n < 2 and n > 0}")

実行結果
$ python3 sample.py 論理演算子 and 0 and 0 = 0 1 and 0 = 0 0 and 1 = 0 1 and 1 = 1 0 and 10 = 0 1 and 10 = 10 n > 2 and n < 0 = False n > 0 and n < -1 = False n > 3 and n < 2 = False n < 2 and n > 0 = True

論理演算子 or


sample.py
print("論理演算子 or\n"); print(f"0 or 0 = {0 or 0}") print(f"1 or 0 = {1 or 0}") print(f"0 or 1 = {0 or 1}") print(f"1 or 1 = {1 or 1}\n") print(f"0 or 10 = {0 or 10}") print(f"1 or 10 = {1 or 10}\n") n = 1 print(f"n > 2 or n < 0 = {n > 2 or n < 0}") print(f"n > 0 or n < -1 = {n > 0 or n < -1}") print(f"n > 3 or n < 2 = {n > 3 or n < 2}") print(f"n < 2 or n > 0 = {n < 2 or n > 0}")

実行結果
$ python3 sample.py 論理演算子 or 0 or 0 = 0 1 or 0 = 1 0 or 1 = 1 1 or 1 = 1 0 or 10 = 10 1 or 10 = 1 n > 2 or n < 0 = False n > 0 or n < -1 = True n > 3 or n < 2 = True n < 2 or n > 0 = True

代入式 :=


sample.py
print("代入式 :=\n"); def func(x): if n := round(x): print(f"func({x}) = {n} * 3 = ", end='') return n * 3 else: print(f"func({x}) = {x} * 2 = ", end='') return x * 2 print(f"{func(1.23)}") print(f"{func(0.23)}")

実行結果
$ python3 sample.py 代入式 := func(1.23) = 1 * 3 = 3 func(0.23) = 0.23 * 2 = 0.46

条件式 if--else


sample.py
print("条件式 if--else\n"); def func(x): print(f"func({x}) : {'x > 0' if x > 0 else 'x <= 0'}") func(0) func(1)

実行結果
$ python3 sample.py 条件式 if--else func(0) : x <= 0 func(1) : x > 0

ラムダ式 lambda


sample.py
print("ラムダ式 lambda\n"); lst = [("a", 3), ("c", 2), ("b", 1), ("b", 3), ("a", 2), ("b", 2)] lst.sort(key=lambda x: (x[0], x[1])) print(f"要素0, 1の順でソート {lst}") lst.sort(key=lambda x: (x[1], x[0])) print(f"要素1, 0の順でソート {lst}")

実行結果
$ python3 sample.py ラムダ式 lambda 要素0, 1の順でソート [('a', 2), ('a', 3), ('b', 1), ('b', 2), ('b', 3), ('c', 2)] 要素1, 0の順でソート [('b', 1), ('a', 2), ('b', 2), ('c', 2), ('a', 3), ('b', 3)]

ジェネレータ式 ( )


sample.py
print("ジェネレータ式\n"); x1 = 1 print(f"x1 : x1 = 1 : x1 = {x1}") print(f" __iter__ : {"有" if "__iter__" in dir(x1) else "無"}") print(f" __next__ : {"有" if "__next__" in dir(x1) else "無"}\n") x2 = ('a', 'b', 'c') print(f"x2 : x2 = ('a', 'b', 'c') : x2 = {x2}") print(f" __iter__ : {"有" if "__iter__" in dir(x2) else "無"}") print(f" __next__ : {"有" if "__next__" in dir(x2) else "無"}\n") x3 = [s for s in x2] print(f"x3 : x3 = [s for s in x2] : x3 = {x3}") print(f" __iter__ : {"有" if "__iter__" in dir(x3) else "無"}") print(f" __next__ : {"有" if "__next__" in dir(x3) else "無"}\n") x4 = (s for s in x2) print(f"x4 : x4 = (s for s in x2) : x4 = {x4}") print(f" __iter__ : {"有" if "__iter__" in dir(x4) else "無"}") print(f" __next__ : {"有" if "__next__" in dir(x4) else "無"}\n")

実行結果
$ python3 sample.py x1 : x1 = 1 : x1 = 1 __iter__ : 無 __next__ : 無 x2 : x2 = ('a', 'b', 'c') : x2 = ('a', 'b', 'c') __iter__ : 有 __next__ : 無 x3 : x3 = [s for s in x2] : x3 = ['a', 'b', 'c'] __iter__ : 有 __next__ : 無 x4 : x4 = (s for s in x2) : x4 = <generator object <genexpr> at 0x7fae2a128ac0> __iter__ : 有 __next__ : 有

実行環境

Python 3.12.11


コード例・出力内容中の表記

・実行例中の太字表記部分は、コマンドなどの入力された文字列を示します。
・「」や「...」の着色省略表記は、 実際のソースコードや出力内容などを省略加工した部分を示します。