python常用运算符和优先级

本文详细介绍了Python中的算术、比较、逻辑及赋值运算符,包括其用法、优先级以及实例演示。重点讲解了不同类型之间的转换规则,并通过示例展示了运算过程中的类型转换和运算结果。此外,还探讨了运算符的优先级,帮助读者更好地理解和运用Python中的运算逻辑。

算术运算符

运算符名称说明例子
+、-、*、/加、减、乘、除字符串之间相加即为连接,字符串与数字相乘即为重复 ,两个数相除结果总为浮点数‘a’+‘b’得到’ab’,‘la’*3得到’lalala’
**指数运算,返回x的y次幂3**4得到81,相当于 pow()函数
//取商数返回商的整数部分(不大于x与y之商的最大整数)4//3.0得到1.0,4//3得到1
%取余数(取模)返回除法的余数(不同语言可能结果不一样,个人理解在python中运算结果是商往负无穷方面取临近整数时的得到余数)-16%3得到2,16%(-3)得到-2,-16%-3得到-1,-25.5%2.25得到1.5

注:
1.三种类型存在一种逐渐扩展或变宽的关系:整数 -> 浮点数 -> 复数
2.类型间混合运算,生成结果为“最宽”的类型。例如整数 + 浮点数 = 浮点数
3.在运算过程中会要求两个操作数类型一致,当操作数类型不一致时,系统会自动将低类型向高类型转换后计算。例如x=2.3;y=5;x+y返回7.3

print(-25.5%2.25)
print(-25.5/2.25)
print(divmod(-25.5,2.25))  #divmod同时取商和余数
print(divmod(25.5,2.25))

输出结果:

1.5
-11.333333333333334
(-12.0, 1.5)
(11.0, 0.75)

比较运算符

运算符名称说明例子
<,>,<=,>=返回False或者True3<5<7返回True
==,!=等于,不等于返回False或者Truex=‘abc’;y='Abc’则x==y返回False

逻辑运算符

运算符名称说明例子
not布尔非返回False或者Truenot 5返回False,not 0返回True
and布尔与返回False或者True1 and 45返回45(两个都是真值时取第二个值)
or布尔或返回False或者TrueTrue or False返回True

注:
1.“与”运算 a and b 时,如果a为假,a and b就返回假,否则返回b的值
2.“或”运算 a or b时,如果a非0,返回a的值,否则返回b的值
3.“非”运算 not a 时,如果a为真,就返回假,如果a为假,就返回真

(巧记: and 和 or 都是前一个值进行决定性判断,如果能对前一个值做出决定性判断则返回前一个值,如果不能得出决定性判断,就返回第二个值。and 前一个为假则必为假,or 前一个为真则必为真,可以把这个理解为决定性判断。)

print(0 and 11)
print(18 and 7)
print(18 and 0)
print(True and False)
print(False and True)

输出结果:

0
7
0
False
False

print(5 or 88)
print(5 or False)
print(0 or 9)
print(False or 0)
print(0 or False)

输出结果:

5
5
9
0
False

赋值运算符

=、+=、-=、*=、/=、%=、**=、//=

>>> a = 3
>>> b = 6
>>> c = 0
>>> c = a + b
>>> c += 5  # 9+5
>>> c
14
>>> c -= a  # 14-3
>>> c
11
>>> c *= a  # 11*3
>>> c
33
>>> c /= a  # 33/3
>>> c
11.0
>>> c %= a  # 11.0/3
>>> c
2.0
>>> c **= a  # 2.0的3次幂
>>> c
8.0
>>> c //=a  # 8.0//3
>>> c
2.0
>>> 

常用运算符的优先级

运算符(从低到高,同一行优先级相同)
lambda 表达式
or 布尔或
and 布尔与
not 布尔非
in、not in(成员测试)
is、is not(同一性测试)
<、<=、>、>=、!=、==
+、-
*、/、%
**

注:
如果想要改变它们的计算顺序,可以使用圆括号()。

  #比较部分优先级
print((1 in [1,0]) == True)  #True
print(1 in [1, 0] == True)  #False
print((2 > 1) is True)  #True
print(2 > 1 is True)  #False

输出结果:

True
False
True
False

<think>我们正在讨论Python中的逻辑运算符优先级问题。逻辑运算符包括and, or, not。它们的优先级为:not > and > or。 同时,我们需要考虑其他运算符(如算术运算符、比较运算符)与逻辑运算符优先级关系。总体优先级顺序为:算术运算符 > 比较运算符 > 逻辑运算符(其中not优先级高于and,and高于or)。 为了解释清楚,我们可以通过一些例题来展示。 示例1:混合算术、比较逻辑运算符 表达式:5 + 3 > 7 and 2 * 2 == 4 or not 1 != 1 计算步骤: 1. 先计算算术运算:5+3=8, 2*2=4 -> 表达式变为:8>7 and 4==4 or not 1!=1 2. 再计算比较运算:8>7为True, 4==4为True, 1!=1为False -> 表达式变为:True and True or not False 3. 然后计算逻辑非:not False -> True -> 表达式变为:True and True or True 4. 逻辑与:True and True -> True 5. 逻辑或:True or True -> True 所以最终结果为True。 示例2:使用括号改变优先级 表达式:not (5 > 3 and 3 < 2) or 4 != 4 计算步骤: 1. 括号内:5>3为True,3<2为False,所以True and False -> False 2. not False -> True 3. 4!=4 -> False 4. True or False -> True 最终结果为True。 示例3:多个逻辑运算符 表达式:True or False and not True 根据优先级,先算not True -> False,然后False and False -> False,最后True or False -> True。 现在,我们可以提供一些练习题供用户练习。 练习题1:计算表达式 10 - 5 == 5 and not 3 > 4 or 2 <= 2 练习题2:计算表达式 (not (5 == 5) and 10 > 5) or 3 != 2 解答: 练习题1: 步骤: 1. 算术运算:10-5=5 -> 表达式变为:5==5 and not 3>4 or 2<=2 2. 比较运算:5==5 -> True, 3>4 -> False, 2<=2 -> True -> 表达式变为:True and not False or True 3. 逻辑非:not False -> True -> 表达式变为:True and True or True 4. 逻辑与:True and True -> True 5. 逻辑或:True or True -> True 结果:True 练习题2: 步骤: 1. 括号内:5==5 -> True,所以not True -> False;10>5 -> True -> 表达式变为:(False and True) or 3!=2 2. 括号内:False and True -> False 3. 比较运算:3!=2 -> True 4. 表达式变为:False or True -> True 结果:True 通过以上示例练习,我们可以更好地理解逻辑运算符优先级。</think>### Python逻辑运算符优先级及示例题目 在Python中,逻辑运算符优先级顺序为:**`not` > `and` > `or`**。当与其他运算符混合使用时,完整的优先级顺序为: 1. 括号 `()` 2. 算术运算符(`**` > `*`、`/`、`%`、`//` > `+`、`-`) 3. 比较运算符(`==`、`!=`、`>`、`<`、`>=`、`<=`) 4. 逻辑运算符(`not` > `and` > `or`) #### 示例题目解析 1. **基础优先级示例** ```python print(True or False and not True) ``` - 步骤解析: - `not True` → `False` - `False and False` → `False`(`and`优先级高于`or`) - `True or False` → `True` - **输出:`True`** 2. **混合算术与逻辑运算** ```python print(10 > 5 + 2 and not 3 == 2 or 4 <= 4) ``` - 步骤解析: - 算术:`5+2=7` → `10>7=True` - 比较:`3==2=False`,`4<=4=True` - 逻辑:`not False=True` → `True and True=True` → `True or True=True` - **输出:`True`** 3. **括号改变优先级** ```python print((5 != 5) or (not (2 > 1 and 3 < 4))) ``` - 步骤解析: - 内层括号:`2>1 and 3<4` → `True and True=True` - `not True=False` - `5!=5=False` - `False or False=False` - **输出:`False`** --- #### 练习题 1. 计算表达式: ```python 8 / 2 == 4 and not 3 > 5 or 2 + 2 != 4 ``` **答案:** `True` *解析:先算`8/2=4`→`4==4=True`,`3>5=False`→`not False=True`,`2+2=4`→`4!=4=False`,最终`True and True or False=True`* 2. 计算表达式: ```python (10 % 3 > 0) or not (7 // 2 == 3 and 5 < 4) ``` **答案:** `True` *解析:`10%3=1>0=True`,`7//2=3`→`3==3=True`,`5<4=False`→`True and False=False`→`not False=True`,最终`True or True=True`* 3. 以下代码输出什么? ```python x = 5 y = 10 print(not x < y or y // x != 2 and x + y == 15) ``` **答案:** `False` *解析:`x<y=True`→`not True=False`,`y//x=2`→`2!=2=False`,`x+y=15`→`15==15=True`,`False and True=False`,最终`False or False=False`* > 关键记忆点: > - **`not` 优先级最高**,可反转布尔值 > - **`and` 类似乘法**,需所有条件为真 > - **`or` 类似加法**,任一条件为真即返回真 > - 使用括号 `()` 可显式控制优先级[^1][^2] --- ### 相关问题 1. Python中比较运算符与逻辑运算符如何配合使用? 2. 如何避免逻辑运算符优先级导致的代码歧义? 3. 身份运算符 `is` 逻辑运算符有何本质区别?[^2] [^1]: 引用自Python基础运算符文档 [^2]: 引用自Python身份运算符解析文档
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值