Python/運算符
外觀
< Python
算術運算符為:+, -, *, / , %, // , **, _ 分別為加、減、乘、浮點除、取餘數、整數除、冪運算、上個表達式結果的值。
比較運算符為:==, !=, >, <, >=, <= 是做 值 的比較
位運算符:&, |, ^, ~ , >>, <<
邏輯運算符: and, or, not
成員運算符: in, not in 在指定的序列中能否找到該值
身份運算符:is, is not 判斷兩個標識符是不是引用自一個對象,類似 id(x) == id(y) 是做地址比較
walrus運算符:在表達式中執行賦值操作。例如:
# Handle a matched regex
if (match := pattern.search(data)) is not None:
# Do something with match
# A loop that can't be trivially rewritten using 2-arg iter()
while chunk := file.read(8192):
process(chunk)
# Reuse a value that's expensive to compute
[y := f(x), y**2, y**3]
# Share a subexpression between a comprehension filter clause and its output
filtered_data = [y for x in data if (y := f(x)) is not None]