Python运算符详解与示例
为了执行各操作python中有很多种运算符,来吧道友一块看看有哪些?
1. 算术运算符
用于基本的数学运算。
运算符 | 描述 | 示例 | 结果 |
+ | 加法 | 5 + 3 | 8 |
- | 减法 | 5 - 3 | 2 |
* | 乘法 | 5 * 3 | 15 |
/ | 除法 | 5 / 2 | 2.5 |
// | 整除 | 5 // 2 | 2 |
% | 取模(余数) | 5 % 2 | 1 |
** | 幂运算 | 2 ** 3 | 8 |
# 算术运算符示例
a = 10
b = 3
print(a + b) # 13
print(a - b) # 7
print(a * b) # 30
print(a / b) # 3.333...
print(a // b) # 3
print(a % b) # 1
print(a ** b) # 1000
2. 比较运算符
用于比较两个值,会返回布尔值(True/False)。
运算符 | 描述 | 示例 | 结果 |
== | 等于 | 5 == 3 | False |
!= | 不等于 | 5 != 3 | True |
> | 大于 | 5 > 3 | True |
< | 小于 | 5 < 3 | False |
>= | 大于等于 | 5 >= 5 | True |
<= | 小于等于 | 5 <= 3 | False |
# 比较运算符示例
x = 5
y = 8
print(x == y) # False
print(x != y) # True
print(x > y) # False
print(x < y) # True
print(x >= 5) # True
print(y <= 10) # True
3. 赋值运算符
用于给变量赋值。
运算符 | 描述 | 等价于 | 示例 |
= | 简单赋值 | x = 5 | x = 5 |
+= | 加法赋值 | x += 3 | x = x + 3 |
-= | 减法赋值 | x -= 3 | x = x - 3 |
*= | 乘法赋值 | x *= 3 | x = x * 3 |
/= | 除法赋值 | x /= 3 | x = x / 3 |
//= | 整除赋值 | x //= 3 | x = x // 3 |
%= | 取模赋值 | x %= 3 | x = x % 3 |
**= | 幂赋值 | x **= 3 | x = x ** 3 |
:= | 海象运算符(Python 3.8+) | 在表达式中赋值 | if (n := len(a)) > 10: |
# 赋值运算符示例
a = 10
a += 5 # a = a + 5
print(a) # 15
b = 20
b /= 4 # b = b / 4
print(b) # 5.0
# 海象运算符示例(Python 3.8+)
names = ["Alice", "Bob", "Charlie", "David"]
if (n := len(names)) > 3:
print(f"List has {n} elements") # List has 4 elements
4. 逻辑运算符
用于组合条件语句。
运算符 | 描述 | 示例 | 结果 |
and | 逻辑与 | True and False | False |
or | 逻辑或 | True or False | True |
not | 逻辑非 | not True | False |
# 逻辑运算符示例
x = True
y = False
print(x and y) # False
print(x or y) # True
print(not x) # False
# 实际应用
age = 25
is_student = False
if age >= 18 and not is_student:
print("可以观看成人内容") # 会执行
5. 位运算符
用于对二进制数进行操作。
运算符 | 描述 | 示例 | 结果 |
& | 按位与 | 5 & 3 | 1 |
| | 按位或 | 5 | 3 | 7 |
^ | 按位异或 | 5 ^ 3 | 6 |
~ | 按位取反 | ~5 | -6 |
<< | 左移 | 5 << 1 | 10 |
>> | 右移 | 5 >> 1 | 2 |
# 位运算符示例
a = 5 # 0101
b = 3 # 0011
print(a & b) # 0001 -> 1
print(a | b) # 0111 -> 7
print(a ^ b) # 0110 -> 6
print(~a) # 1010 -> -6 (二进制补码表示)
print(a << 1) # 1010 -> 10
print(a >> 1) # 0010 -> 2
6. 成员运算符
测试序列中是否包含某个成员。
运算符 | 描述 | 示例 | 结果 |
in | 如果在序列中找到值 | 5 in [1,2,3,4,5] | True |
not in | 如果在序列中没有找到值 | 6 not in [1,2,3] | True |
# 成员运算符示例
fruits = ['apple', 'banana', 'cherry']
print('banana' in fruits) # True
print('orange' not in fruits) # True
# 字符串也可以使用
s = "Hello World"
print('World' in s) # True
7. 身份运算符
比较对象的内存地址(是否为同一对象)。
运算符 | 描述 | 示例 | 结果 |
is | 如果两个变量是同一个对象 | x is y | False |
is not | 如果两个变量不是同一个对象 | x is not y | True |
# 身份运算符示例
a = [1, 2, 3]
b = a
c = [1, 2, 3]
print(a is b) # True, 同一个对象
print(a is c) # False, 内容相同但不是同一个对象
print(a == c) # True, 内容相同
# 小整数池现象(仅适用于小整数)
x = 256
y = 256
print(x is y) # True
x = 257
y = 257
print(x is y) # False (在交互式环境中可能为True)
8. 运算符优先级
当表达式中有多个运算符时,Python会按照优先级顺序进行计算。以下是优先级从高到低的顺序:
- () - 括号
- ** ** - 指数
- ~ + - - 按位取反, 正负号
- ** * / % //** - 乘, 除, 取模, 整除
- ** + -** - 加, 减
- ** << >>** - 位移
- ** &** - 按位与
- ** ^ |** - 按位异或, 按位或
- ** <= < > >=** - 比较运算符
- ** == !=** - 等于, 不等于
- ** = %= /= //= -= += *= = - 赋值运算符
- is is not - 身份运算符
- in not in - 成员运算符
- not or and - 逻辑运算符
# 运算符优先级示例
result = 5 + 3 * 2 ** 2 / 4 - 1
# 计算顺序:
# 1. 2 ** 2 = 4
# 2. 3 * 4 = 12
# 3. 12 / 4 = 3.0
# 4. 5 + 3.0 = 8.0
# 5. 8.0 - 1 = 7.0
print(result) # 7.0
# 使用括号改变优先级
result = (5 + 3) * (2 ** (2 / 4)) - 1
print(result) # 约7.656
9. 三元运算符
Python中的三元运算符是一种简洁的条件表达式。
语法:[on_true] if [expression] else [on_false]
# 三元运算符示例
age = 20
status = "Adult" if age >= 18 else "Minor"
print(status) # Adult
# 等同于
if age >= 18:
status = "Adult"
else:
status = "Minor"
10. 运算符重载
Python允许类通过特殊方法重载运算符。
# 运算符重载示例
class Vector:
def __init__(self, x, y):
self.x = x
self.y = y
def __add__(self, other):
return Vector(self.x + other.x, self.y + other.y)
def __str__(self):
return f"Vector({self.x}, {self.y})"
v1 = Vector(2, 3)
v2 = Vector(4, 5)
print(v1 + v2) # Vector(6, 8)
上面就是主要的运算符及其使用方法,也是必须要掌握的,优先级:先乘除后加减