Compare... Closelier

本文详细介绍了Python中运算符的优先级,并通过实例演示了如何正确使用这些运算符进行表达式的构造。文章强调了理解运算符优先级对于避免程序错误的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Compare... Closelier

Excellent! It looks like you're comfortable with basic expressions and comparators.

But what about... extreme expressions and comparators?

(This exercise may seem unnecessary to you, but we can't tell you the number of problems caused in programs by incorrect order of operations or reversed >s and<s. Bugs like this can be a serious problem!)

这个表给出Python的运算符优先级(从低到高).

从最低的优先级(最松散地结合)到最高的优先级(最紧密地结合)。

这意味着在一个表达式中,Python会首先计算表中较下面的运算符,然后在计算列在表上部的运算符。

Python 运算符优先级
运算符描述
lambdaLambda表达式
or布尔“或”
and布尔“与”
not x布尔“非”
in,not in成员测试
is,is not同一性测试
<,<=,>,>=,!=,==比较
|按位或
^按位异或
&按位与
<<,>>移位
+,-加法与减法
*,/,%乘法、除法与取余
+x,-x正负号
~x按位翻转
**指数
x.attribute属性参考
x[index]下标
x[index:index]寻址段
f(arguments...)函数调用
(experession,...)绑定或元组显示
[expression,...]列表显示
{key:datum,...}字典显示
'expression,...'字符串转换

# Assign True or False as appropriate on the lines below!

# 20 + -10 * 2 > 10 % 3 % 2
bool_one = 20 + -10 * 2 > 10 % 3 % 2

# (10 + 17)**2 == 3**6
bool_two = (10 + 17)**2 == 3**6

# 1**2**3 <= -(-(-1))
bool_three = 1**2**3 <= -(-(-1))

# 40 / 20 * 4 >= -4**2
bool_four = 40 / 20 * 4 >= -4**2

# 100**0.5 != 6 + 4
bool_five = 100**0.5 != 6 + 4
##程序中涉及到的算术运算符、比较运算符和赋值运算符,优先级由表可知算术运算符>比较运算符>赋值运算符
print("bool_one:%s\nbool_two:%s\nbool_three:%s\nbool_four:%s\nbool_five:%s\n"%(bool_one,bool_two,bool_three,bool_four,bool_five))



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值