Python 运算符终极指南:从基础运算到高级技巧,一篇全搞定
·
1. 算术运算符
算术运算符用于执行数学运算,是最基础的运算符类型。
| 运算符 | 名称 | 示例 | 结果 |
|---|---|---|---|
+ |
加法 | 5 + 3 |
8 |
- |
减法 | 5 - 3 |
2 |
* |
乘法 | 5 * 3 |
15 |
/ |
除法(浮点) | 5 / 2 |
2.5 |
// |
整除(向下取整) | 5 // 2 |
2 |
% |
取模(取余) | 5 % 2 |
1 |
** |
幂运算 | 5 ** 3 |
125 |
# 算术运算符示例
a, b = 10, 3
print(f"加法: {a} + {b} = {a + b}") # 13
print(f"减法: {a} - {b} = {a - b}") # 7
print(f"乘法: {a} * {b} = {a * b}") # 30
print(f"除法: {a} / {b} = {a / b}") # 3.333...
print(f"整除: {a} // {b} = {a // b}") # 3
print(f"取模: {a} % {b} = {a % b}") # 1
print(f"幂运算: {a} ** {b} = {a ** b}") # 1000
📌 特别注意
# 除法总是返回浮点数
print(10 / 5) # 2.0,不是 2
# 整除向下取整(向负无穷方向)
print(10 // 3) # 3
print(-10 // 3) # -4 (注意不是 -3)
# 字符串的加法与乘法
print("Hello" + " " + "World") # 字符串拼接 Hello World
print("-" * 20) # 字符串重复 --------------------
2. 赋值运算符
赋值运算符用于为变量赋值,其中最常用的是 =。
| 运算符 | 等价于 | 示例 |
|---|---|---|
= |
赋值 | x = 5 |
+= |
x = x + n |
x += 3 |
-= |
x = x - n |
x -= 3 |
*= |
x = x * n |
x *= 3 |
/= |
x = x / n |
x /= 3 |
//= |
x = x // n |
x //= 3 |
%= |
x = x % n |
x %= 3 |
**= |
x = x ** n |
x **= 3 |
&= |
x = x & n |
x &= 3 |
|= |
x = x | n |
x |= 3 |
^= |
x = x ^ n |
x ^= 3 |
>>= |
x = x >> n |
x >>= 3 |
<<= |
x = x << n |
x <<= 3 |
# 赋值运算符示例
x = 10
print(f"初始: x = {x}")
x += 5
print(f"x += 5 → {x}") # 15
x -= 3
print(f"x -= 3 → {x}") # 12
x *= 2
print(f"x *= 2 → {x}") # 24
x /= 4
print(f"x /= 4 → {x}") # 6.0
x //= 2
print(f"x //= 2 → {x}") # 3.0
🔥 链式赋值与解包赋值
# 链式赋值
a = b = c = 10
print(a, b, c) # 10 10 10
# 元组解包赋值
x, y = 5, 10
x, y = y, x # 交换变量值
print(f"x = {x}, y = {y}") # x = 10, y = 5
# 列表解包
numbers = [1, 2, 3]
p, q, r = numbers
print(p, q, r) # 1 2 3
# 星号解包(Python 3.5+)
first, *middle, last = [1, 2, 3, 4, 5]
print(first, middle, last) # 1 [2, 3, 4] 5
3. 比较运算符
比较运算符用于比较两个值,返回布尔值(True 或 False)。
| 运算符 | 含义 | 示例 | 结果 |
|---|---|---|---|
== |
等于 | 5 == 5 |
True |
!= |
不等于 | 5 != 3 |
True |
> |
大于 | 5 > 3 |
True |
< |
小于 | 5 < 3 |
False |
>= |
大于等于 | 5 >= 5 |
True |
<= |
小于等于 | 5 <= 3 |
False |
# 比较运算符示例
a, b = 10, 20
print(f"a == b: {a == b}") # False
print(f"a != b: {a != b}") # True
print(f"a > b: {a > b}") # False
print(f"a < b: {a < b}") # True
print(f"a >= b: {a >= b}") # False
print(f"a <= b: {a <= b}") # True
📌 链式比较
Python 支持链式比较,使代码更加简洁优雅:
x = 5
# 普通写法
if x > 0 and x < 10:
print("x 在 0 到 10 之间")
# 链式比较(推荐)
if 0 < x < 10:
print("x 在 0 到 10 之间")
# 更多示例
age = 25
if 18 <= age <= 60:
print("符合工作年龄")
# 比较列表、元组等可迭代对象(按字典序比较)
print([1, 2, 3] < [1, 2, 4]) # True
print((1, 2) < (1, 3)) # True
4. 逻辑运算符
逻辑运算符用于组合布尔表达式,返回布尔值。
| 运算符 | 含义 | 示例 | 结果 |
|---|---|---|---|
and |
逻辑与(且) | True and False |
False |
or |
逻辑或(或) | True or False |
True |
not |
逻辑非(取反) | not True |
False |
# 逻辑运算符示例
a, b = True, False
print(f"a and b: {a and b}") # False
print(f"a or b: {a or b}") # True
print(f"not a: {not a}") # False
print(f"not b: {not b}") # True
# 结合比较运算符
age = 25
is_student = True
if age >= 18 and is_student:
print("符合学生优惠条件")
if not is_student:
print("不是学生")
⚡ 短路求值(Short-Circuit Evaluation)
Python 的逻辑运算符采用短路求值策略:
# and:第一个为 False 则不计算第二个
def expensive_operation():
print("执行了耗时的操作")
return True
result = False and expensive_operation() # expensive_operation 不会执行
print(result) # False
# or:第一个为 True 则不计算第二个
result = True or expensive_operation() # expensive_operation 不会执行
print(result) # True
🎯 逻辑运算符的返回值
Python 的 and 和 or 返回的是最后一个被计算的操作数的值,不一定是布尔值:
# and 返回第一个为 False 的值,否则返回最后一个值
print(0 and 10) # 0
print(5 and 10) # 10
print(5 and 0 and 10) # 0
# or 返回第一个为 True 的值,否则返回最后一个值
print(0 or 10) # 10
print(5 or 10) # 5
print(0 or "" or []) # []
# 实用场景:默认值赋值
name = input("请输入名字: ") or "匿名用户"
print(f"你好, {name}!")
5. 其他运算符
5.1 成员运算符
| 运算符 | 含义 | 示例 |
|---|---|---|
in |
在...中 | 'a' in 'abc' |
not in |
不在...中 | 'd' not in 'abc' |
# 成员运算符
fruits = ['apple', 'banana', 'orange']
print('apple' in fruits) # True
print('grape' in fruits) # False
print('grape' not in fruits) # True
# 字符串成员检查
text = "Hello, World!"
print("World" in text) # True
print("Python" in text) # False
# 字典成员检查(检查键)
person = {'name': 'Alice', 'age': 30}
print('name' in person) # True
print('Alice' in person) # False(检查键,不检查值)
5.2 身份运算符
| 运算符 | 含义 | 示例 |
|---|---|---|
is |
是同一个对象 | a is b |
is not |
不是同一个对象 | a is not b |
# 身份运算符
a = [1, 2, 3]
b = [1, 2, 3]
c = a
print(a == b) # True(值相等)
print(a is b) # False(不同对象)
print(a is c) # True(同一个对象)
# 与 None 比较(推荐使用 is)
x = None
if x is None:
print("x 是 None")
# 注意:小整数缓存机制
x = 256
y = 256
print(x is y) # True(Python 缓存 -5 到 256 的整数)
x = 257
y = 257
print(x is y) # False(超出缓存范围,不同对象)
5.3 位运算符
| 运算符 | 名称 | 示例 |
|---|---|---|
& |
按位与 | 5 & 3 → 1 |
| |
按位或 | 5 | 3 → 7 |
^ |
按位异或 | 5 ^ 3 → 6 |
~ |
按位取反 | ~5 → -6 |
<< |
左移 | 5 << 1 → 10 |
>> |
右移 | 5 >> 1 → 2 |
# 位运算符示例
a, b = 5, 3 # 0101, 0011
print(f"a & b = {a & b}") # 1 (0001)
print(f"a | b = {a | b}") # 7 (0111)
print(f"a ^ b = {a ^ b}") # 6 (0110)
print(f"~a = {~a}") # -6
print(f"a << 1 = {a << 1}") # 10 (1010)
print(f"a >> 1 = {a >> 1}") # 2 (0010)
6. 运算符优先级
了解运算符优先级可以避免写出歧义的表达式:
| 优先级(从高到低) | 运算符 |
|---|---|
| 1 | () 括号 |
| 2 | ** 幂运算 |
| 3 | +x, -x, ~x 正负号、按位取反 |
| 4 | *, /, //, % 乘除取余 |
| 5 | +, - 加减 |
| 6 | <<, >> 移位 |
| 7 | & 按位与 |
| 8 | ^ 按位异或 |
| 9 | | 按位或 |
| 10 | ==, !=, >, <, >=, <=, is, in 比较 |
| 11 | not 逻辑非 |
| 12 | and 逻辑与 |
| 13 | or 逻辑或 |
| 14 | = 赋值(最低) |
# 优先级示例
x = 5 + 3 * 2 # 先乘后加:5 + 6 = 11
y = (5 + 3) * 2 # 括号优先:8 * 2 = 16
# 复杂表达式建议使用括号增强可读性
result = (a + b) * c / (d - e)
📝 总结
| 运算符类别 | 核心要点 |
|---|---|
| 算术 | 注意 / 返回浮点数,// 向下取整 |
| 赋值 | 支持链式赋值和解包赋值 |
| 比较 | 支持链式比较如 0 < x < 10 |
| 逻辑 | 短路求值特性,返回操作数值而非布尔值 |
| 成员 | in 检查序列/字典是否包含元素 |
| 身份 | is 检查对象身份,与 == 不同 |
更多推荐



所有评论(0)