Python3 运算符
Arithmetic, comparison chaining, bitwise, logical, membership and identity operators.
Key Points`==` compares values, `is` compares identity; `and`/`or` return operands, not booleans.
Arithmetic
| Operator | Meaning | Example |
|---|---|---|
| + - * | Add, subtract, multiply | 2 * 3 -> 6 |
| / | True division, always float | 7 / 2 -> 3.5 |
| // | Floor division, rounds down | -7 // 2 -> -4 |
| % | Modulo, sign follows divisor | -7 % 3 -> 2 |
| ** | Power | 2 ** 10 -> 1024 |
| := | Walrus: assign inside expression | if (n := len(s)) > 3: |
Python
print(7 / 2, 7 // 2, 7 % 2) # 3.5 3 1
print(-7 // 2, -7 % 2) # -4 1
# 海象运算符:少写一次调用
data = "abcdef"
if (n := len(data)) > 5:
print(f"too long: {n}")Comparison Chaining
Python allows mathematical chaining: 0 < x < 10 means 0 < x and x < 10, and x is evaluated only once.
Python
age = 25
print(18 <= age < 60) # True
print(1 < 2 < 3 < 4) # TrueBitwise
| Operator | Meaning | Example |
|---|---|---|
| & | AND | 0b1100 & 0b1010 -> 8 |
| | | OR | 0b1100 | 0b1010 -> 14 |
| ^ | XOR | 0b1100 ^ 0b1010 -> 6 |
| ~ | NOT: ~x == -x-1 | ~5 -> -6 |
| << >> | Shift left / right | 1 << 10 -> 1024 |
Python
# 权限位:读 4 / 写 2 / 执行 1
READ, WRITE, EXEC = 4, 2, 1
perm = READ | WRITE # 6
print(bool(perm & WRITE)) # True
perm &= ~WRITE # 撤销写权限 -> 4
print(bin(perm))Logical, Membership, Identity
| Operator | Returns | Note |
|---|---|---|
| and / or | An operand, not a bool | a or b is a if a is truthy |
| not | Always a bool | not [] -> True |
| in / not in | bool | Works on str, list, dict, set |
| is / is not | bool | Compares identity (same object) |
Python
name = input("name: ") or "anonymous" # 空输入时取默认值
print("py" in "python", 3 in [1, 2, 3]) # True True
a = [1, 2, 3]
b = [1, 2, 3]
print(a == b, a is b) # True False —— 值相等但不是同一对象
print(a is None, a == None) # False False —— 判空请用 is NoneOnly use
is for None, True, False and sentinel objects. Comparing strings with is sometimes works by interning and then breaks in production.Precedence
**— power (right associative)* / // %then+ -<< >>then&then^then|- Comparisons,
in,is,not andthenorthen:=
When in doubt, add parentheses.
a and b or c is not a ternary — it breaks when b is falsy.算术运算符
| 运算符 | 含义 | 示例 |
|---|---|---|
| + - * | 加、减、乘 | 2 * 3 -> 6 |
| / | 真除法,结果总是 float | 7 / 2 -> 3.5 |
| // | 向下取整除法 | -7 // 2 -> -4 |
| % | 取余,符号随除数 | -7 % 3 -> 2 |
| ** | 幂 | 2 ** 10 -> 1024 |
| := | 海象:表达式内赋值 | if (n := len(s)) > 3: |
Python
print(7 / 2, 7 // 2, 7 % 2) # 3.5 3 1
print(-7 // 2, -7 % 2) # -4 1
# 海象运算符:少写一次调用
data = "abcdef"
if (n := len(data)) > 5:
print(f"too long: {n}")链式比较
Python 支持数学式链式比较:0 < x < 10 等价于 0 < x and x < 10,且 x 只求值一次。
Python
age = 25
print(18 <= age < 60) # True
print(1 < 2 < 3 < 4) # True位运算
| 运算符 | 含义 | 示例 |
|---|---|---|
| & | 按位与 | 0b1100 & 0b1010 -> 8 |
| | | 按位或 | 0b1100 | 0b1010 -> 14 |
| ^ | 按位异或 | 0b1100 ^ 0b1010 -> 6 |
| ~ | 取反:~x == -x-1 | ~5 -> -6 |
| << >> | 左移 / 右移 | 1 << 10 -> 1024 |
Python
# 权限位:读 4 / 写 2 / 执行 1
READ, WRITE, EXEC = 4, 2, 1
perm = READ | WRITE # 6
print(bool(perm & WRITE)) # True
perm &= ~WRITE # 撤销写权限 -> 4
print(bin(perm))逻辑、成员、身份
| 运算符 | 返回 | 备注 |
|---|---|---|
| and / or | 某个操作数,不是布尔值 | a or b 在 a 为真时返回 a |
| not | 总是布尔值 | not [] -> True |
| in / not in | 布尔值 | 适用于 str、list、dict、set |
| is / is not | 布尔值 | 比较身份(同一对象) |
Python
name = input("name: ") or "anonymous" # 空输入时取默认值
print("py" in "python", 3 in [1, 2, 3]) # True True
a = [1, 2, 3]
b = [1, 2, 3]
print(a == b, a is b) # True False —— 值相等但不是同一对象
print(a is None, a == None) # False False —— 判空请用 is Noneis 只用于 None、True、False 和哨兵对象。用 is 比较字符串有时靠驻留机制「恰好」成立,到线上就会出错。运算符优先级
**—— 幂(右结合)* / // %,然后+ -<< >>,然后&,然后^,最后|- 比较、
in、is、not and,然后or,最后:=
拿不准就加括号。
a and b or c 不是三元表达式 —— b 为假时会出错。