关键点:比较运算符不仅可用于数值,还可用于字符串、列表等可比较的数据类型。
3. 赋值运算符
赋值运算符用于给变量赋值或更新变量值。
# 基础赋值counter = 0 # 简单赋值# 复合赋值运算符counter += 5 # 相当于 counter = counter + 5counter -= 2 # 相当于 counter = counter - 2counter *= 3 # 相当于 counter = counter * 3counter /= 2 # 相当于 counter = counter / 2counter %= 4 # 相当于 counter = counter % 4counter **= 2 # 相当于 counter = counter ** 2counter //= 3 # 相当于 counter = counter // 3
在 Python 3.8 及更高版本中,引入了一种新的语法特性,称为"海象运算符",它使用 := 符号。这个运算符的主要目的是在表达式中同时进行赋值和返回赋值的值。
# 海象运算符(Python 3.8+)# 传统写法data = [1, 2, 3, 4, 5]n = len(data)if n > 3:print(f"列表有{n}个元素")# 使用海象运算符简化if (n := len(data)) > 3:print(f"列表有{n}个元素")
if (n := len(data)) > 3:这是使用海象运算符(:=)的写法。海象运算符在表达式中进行赋值操作。
n := len(data):将变量 len(data)赋值给n,同时返回这个赋值结果。
> 3:检查赋值后的 n 是否大于 3。如果条件为真,则执行接下来的代码块。
print(f"列表有{n}个元素"):如果条件为真,打印列表有5个元素。
海象运算符的优点:
4. 位运算符
位运算符直接操作整数的二进制位,适用于底层编程和性能优化场景。
a = 30 # 二进制:0001 1110b = 23 # 二进制:0001 0111print(a & b) # 22 (0001 0110) - 按位与print(a | b) # 31 (0001 1111) - 按位或print(a ^ b) # 9 (0000 1001) - 按位异或print(~a) # -31 (1110 0001) - 按位取反print(a << 2) # 120 (0111 1000) - 左移2位print(a >> 2) # 7 (0000 0111) - 右移2位
实际应用:位运算符常用于权限系统、标志位处理、数据压缩和加密算法等场景。
5. 逻辑运算符
Python有三个逻辑运算符:and(与)、or(或)、not(非)。它们用于组合或修改布尔值(True/False)。🚀and 运算符(两个条件都为True时返回True)
#基本用法print(True and True) # Trueprint(True and False) # Falseprint(False and True) # Falseprint(False and False) # False
🔥or 运算符(至少一个条件为True时返回True)
# 基本用法print(True or True) # Trueprint(True or False) # Trueprint(False or True) # Trueprint(False or False) # False
⚡️not 运算符(反转布尔值)
# 基本用法print(not True) # Falseprint(not False) # True
💡组合使用示例
# 简单组合示例x = 5# and 和 or 组合result1 = (x > 0) and (x < 10) # True: 5大于0且小于10print(f"x在0和10之间: {result1}")result2 = (x < 0) or (x > 10) # False: 5不小于0也不大于10print(f"x小于0或大于10: {result2}")# 使用 notresult3 = not (x > 10) # True: 5不大于10,所以not后为Trueprint(f"x不大于10: {result3}")
📊真值表参考
🤖重要特性:Python逻辑运算符具有短路求值特性,即一旦能确定整个表达式的结果,就不再计算剩余部分。
6. 成员运算符
成员运算符用于测试序列(字符串、列表、元组、字典、集合)中是否包含某个元素。
# 成员运算符实例fruits = ["apple", "banana", "orange", "grape"]# in运算符print("apple" in fruits) # Trueprint("pear" in fruits) # False# not in运算符print("pear" not in fruits) # Trueprint("apple" not in fruits) # False# 字符串中的成员测试text = "Hello, World!"print("World" in text) # Trueprint("Python" not in text) # True# 字典中测试键(不是值)person = {"name": "Alice", "age": 30}print("name" in person) # Trueprint("Alice" in person) # False(只检查键)
7. 身份运算符
身份运算符比较两个对象的内存地址(是否引用同一对象),而不仅仅是值。
# 身份运算符实例list1 = [1, 2, 3]list2 = [1, 2, 3]list3 = list1 # list3引用list1的同一对象print(list1 == list2) # True(值相等)print(list1 is list2) # False(不是同一对象)print(list1 is list3) # True(是同一对象)print(list1 is not list2) # Trueprint(list1 is not list3) # False# 小整数缓存现象(Python优化)a = 10b = 10print(a is b) # True(对于-5到256的小整数,Python会缓存)x = 1000y = 1000print(x is y) # False(大整数不会缓存,除非同一表达式)
三、运算符优先级
运算符优先级从高到低,如下:
() - 括号(最高优先级)
** - 乘方
~ + - - 按位取反、正负号
* / % // - 乘、除、取模、整除
+ - - 加、减
<< >> - 位移
& - 按位与
^ | - 按位异或、或
<= < > >= - 比较
== != - 等于、不等于
= %= /= //= -= += *= **= - 赋值
is is not - 身份
in not in - 成员
not and or - 逻辑(最低)