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会首先计算表中较下面的运算符,然后在计算列在表上部的运算符。
运算符 | 描述 |
---|---|
lambda | Lambda表达式 |
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))
