美文网首页Python运算符
Python运算符(8)-运算符优先级

Python运算符(8)-运算符优先级

作者: 复苏的兵马俑 | 来源:发表于2020-03-14 20:41 被阅读0次

Python语言支持以下类型的运算符:
  1、算术运算符
  2、比较(关系)运算符
  3、赋值运算符
  4、逻辑运算符
  5、位运算符
  6、成员运算符
  7、身份运算符
  8、运算符优先级

8、运算符优先级

以下表格列出了从最高到最低优先级的所有运算符:

运算符 描述 结合性
() 小括号
x[i] 或 x[i1: i2 [:i3]] 索引运算符
x.attribute 属性访问
** 指数 (最高优先级)
~,+,- 按位翻转, 一元加号和减号 (最后两个的方法名为 +@ 和 -@)
*,/,%,// 乘,除,取模和取整除
+,- 加法减法
>>,<< 右移,左移运算符
& 位 'AND'
^,| 位运算符
>=,>,<,<= 比较运算符
==,!= 等于运算符
=,%=,/=,//=,-=,+=,=,*= 赋值运算符
is,is not 身份运算符
in,not in 成员运算符
not 逻辑运算符 “非”
and 逻辑运算符 “与”
or 逻辑运算符 “或 ”(最低优先级)
exp1, exp2 逗号运算符

  *注:所谓结合性,就是当一个表达式中出现多个优先级相同的运算符时,先执行哪个运算符:先执行左边的叫左结合性,先执行右边的叫右结合性。

实例代码:

print('“(20 + 10) * 15 / 5” 运算结果为:{}'.format((20 + 10) * 15 / 5))
print('“((20 + 10) * 15) / 5” 运算结果为:{}'.format(((20 + 10) * 15) / 5))
print('“(20 + 10) * (15 / 5)” 运算结果为:{}'.format((20 + 10) * (15 / 5)))
print('“20 + (10 * 15) / 5” 运算结果为:{}'.format(20 + (10 * 15) / 5))
print('“2 > 1 and 1 < 4”的返回值为:{}'.format(2 > 1 and 1 < 4))
print('“2 > 1 and 1 < 4 or 2 < 3 and 9 > 6 or 2 < 4 and 3 < 2”的返回值为:{}'.format(2 > 1 and 1 < 4 or 2 < 3 and 9 > 6 or 2 < 4 and 3 < 2))
print('“3>4 or 4<3 and 1==1”的返回值为:{}'.format(3>4 or 4<3 and 1==1))
print('“1 < 2 and 3 < 4 or 1>2”的返回值为:{}'.format(1 < 2 and 3 < 4 or 1>2))
print('“2 > 1 and 3 < 4 or 4 > 5 and 2 < 1”的返回值为:{}'.format(2 > 1 and 3 < 4 or 4 > 5 and 2 < 1))
print('“1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8”的返回值为:{}'.format(1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8))
print('“1 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6”的返回值为:{}'.format(1 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6))
print('“not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6”的返回值为:{}'.format(not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6))

运行结果:

“(20 + 10) * 15 / 5” 运算结果为:90.0
“((20 + 10) * 15) / 5” 运算结果为:90.0
“(20 + 10) * (15 / 5)” 运算结果为:90.0
“20 + (10 * 15) / 5” 运算结果为:50.0
“2 > 1 and 1 < 4”的返回值为:True
“2 > 1 and 1 < 4 or 2 < 3 and 9 > 6 or 2 < 4 and 3 < 2”的返回值为:True
“3>4 or 4<3 and 1==1”的返回值为:False
“1 < 2 and 3 < 4 or 1>2”的返回值为:True
“2 > 1 and 3 < 4 or 4 > 5 and 2 < 1”的返回值为:True
“1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8”的返回值为:False
“1 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6”的返回值为:False
“not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6”的返回值为:False

相关文章

  • # Python 中的运算符

    Python中常用的运算符算数运算符标准运算符取余运算符幂运算赋值运算符比较运算符布尔运算符位运算符运算符的优先级...

  • Python运算符的优先级

    python中运算符的优先级示意图 python中运算符的优先级口诀 函数寻址下标一 幂运算小二小嘻嘻 全体单元第...

  • JS笔记8:运算符及优先级

    1. 运算符 2. 运算符优先级 优先级运算符优先级 口诀括号一, //括号运算符[] ()单目二;...

  • 二进制 & 位运算

    python 运算符优先级 运算符描述**指数 (最高优先级)~ + -按位翻转, 一元加号和减号 (最后两个的方...

  • 03-基本运算

    运算符分为:算术运算符、比较运算符和逻辑运算符优先级是:算术运算符>比较运算符>逻辑运算符 注意:没记住优先级的话...

  • C/C++学习笔记

    C/C++运算符优先级 简单记就是:! > 算术运算符 > 关系运算符 > && > || > 赋值运算符同优先级...

  • Python基础篇之运算符

    主要介绍Python的逻辑运算符以及其优先级 逻辑运算符 运算符解释案例单运算符+加法a+b 均为数字两者求和,均...

  • 运算符优先级和结合性

    运算符优先级和结合性 运算符的优先级使得一些运算符优于其他运算符,高优先级的运算符会被先计算。 结合性定义了具有相...

  • C程序设计语言 2.6 关系运算符与逻辑运算符

    关系运算符包括以下几个运算符: 其中,==,!=比> >= < <=优先级低,关系运算符的优先级比算术运算符低。因...

  • C语言中的运算符和运算符的优先级

    运算符 运算符的优先级

网友评论

    本文标题:Python运算符(8)-运算符优先级

    本文链接:https://www.haomeiwen.com/subject/tnvnshtx.html