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]