{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 运算优先级"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Python支持多种运算符的混合运算,所有运算符的优先级(由高到低排列)的描述如下表所示。"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"| 序号 | 运算符 | 描述 |\n",
"| :----: | :-------------------------: | :-------------------------------------- |\n",
"| 1 | ()、\\[\\]、{} | <font color=Red>__括号表达式,元组、列表、字典、集合__</font> |\n",
"| 2 | x\\[i\\],x\\[m:n\\] | 索引、切片 |\n",
"| 3 | \\*\\* | <font color=Red>__幂运算__</font> |\n",
"| 4 | +x、 -x、~x | 正、负,按位非 NOT |\n",
"| 5 | \\*、 / 、//、% | <font color=Red>__乘法、除法与取模__</font> |\n",
"| 6 | + 、- | <font color=Red>__加法与减__</font>法 |\n",
"| 7 | << 、>> | 移位 |\n",
"| 8 | & | 按位与 |\n",
"| 9 | ^ | 按位异或 |\n",
"| 10 | \\| | 按位或 |\n",
"|11 | <、<=、>、>=、!=、== 、is、is not、in、not in | <font color=Red>__比较运算、成员检测和标识号检测__</font> |\n",
"| 12 | not x | 逻辑非 |\n",
"| 13 | and | <font color=Red>__逻辑与运算符__</font> |\n",
"| 14 | or | <font color=Red>__逻辑或运算符__</font> |\n",
"| 15 | if…else | 条件表达式 |\n",
"| 16 | lambda | lambda表达式 |\n",
"| 17 | := | <font color=Red>__赋值表达式、海象运算符__</font> |"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(4 * 2 ** 3) # 先幂运算,再计算乘法,输出:32\n",
"print(3 + 4 * -2) # 2先取反、与4相乘,再做加法,输出:-5\n",
"print(3 + 4 * 2 / 2) # 先计算乘除法,除法运算结果为浮点数,输出:7.0\n",
"print(3 << 2 + 1) # 加法优先级高,先2+1,3(11)左移3位变成24 (11000)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"括号的优先级最高,可以强制表达式按照需要的顺序求值。可以通过加入小括号“()”的方法来提供弱优先级的优先执行。加了括号,无需比较哪个优先级更高,使程序和表达式更加易于阅读和维护。"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"## 实例:判断是否直角三角形\n",
"输入三个数a,b,c, 判断能否以它们为三个边长构成直角三角形。若能,输出YES,否则输出NO。\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"a = float(input())\n",
"b = float(input())\n",
"c = float(input())\n",
"\n",
"# 三角形判定条件:边都为正数,任意两边之和大于第三边\n",
"if a <= 0 or b <= 0 or c <= 0 or (a + b) <= c or (a + c) <= b or (b + c) <= a:\n",
" print('No')\n",
"# 直角三角形条件,两边平方和等于第三边\n",
"elif a * a + b * b == c * c or a * a + c * c == b * b or c * c + b * b == a * a:\n",
" print('Yes')\n",
"else:\n",
" print('No')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"上面的代码中条件都比较长,可通过改进算法简化,代码如下。"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"a = float(input())\n",
"b = float(input())\n",
"c = float(input())\n",
"\n",
"shortest = min(a, b, c) # 获得最短边长度\n",
"longest = max(a, b, c) # 获得最长边长度\n",
"middle = sum([a, b, c]) - shortest - longest\n",
"\n",
"# 若最小边为负值或长边小于等于其他两边之和时,构不成三角形\n",
"if shortest <= 0 or shortest + middle <= longest:\n",
" print('NO')\n",
" \n",
"# 两边短边长度平方和等于第三边长度平方为直角三角形\n",
"elif shortest ** 2 + middle ** 2 == longest ** 2:\n",
" print('YES')\n",
" \n",
"else:\n",
" print('NO')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}