博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
运算符
阅读量:4647 次
发布时间:2019-06-09

本文共 1895 字,大约阅读时间需要 6 分钟。

Python运算符优先级

1、and比or的优先级

在没有()的情况下not 优先级高于 and,and优先级高于or,即优先级关系为( )>not>and>or,同一优先级从左往右计算。

运算符 描述
** 指数 (最高优先级)
~ + - 按位翻转, 一元加号和减号 (最后两个的方法名为 +@ 和 -@)
* / % // 乘,除,取模和取整除
+ - 加法减法
>> << 右移,左移运算符
& 位 'AND'
^ | 位运算符
<= < > >= 比较运算符
<> == != 等于运算符
= %= /= //= -= += *= **= 赋值运算符
is is not 身份运算符
in not in 成员运算符
not and or 逻辑运算符

以下几个例子

1 print(3>4 or 4<3 and 1==1) 2 print(1 < 2 and 3 < 4 or 1>2) 3 print(2 > 1 and 3 < 4 or 4 > 5 and 2 < 1) 4 print(1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8) 5 print(1 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6) 6 print(not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6) 7  8 输出结果: 9 False10 True11 True12 False13 False14 False
View Code

 2、or  及 and的运算结果:

x or y , x为真,值就是x,x为假,值是y;

x and y, x为真,值是y,x为假,值是x。

例如:

1 m1 = 8 or 4 2 m2 = 0 and 3 3 m3 = 0 or 4 and 3 or 7 or 9 and 6 4 print(m1) 5 print(m2) 6 print(m3) 7  8 结果为: 9 810 011 3
View Code

3、成员运算符  in、not in

可以用在子字符串是否在目标字符串(包括元组、字典、集合等)中

例如:

1 print("早起" in "我每天都早起") 2 print("peng" in "pengjunfei") 3 print("peng" in "junfei") 4 print("peng" not in "junfei") 5  6 结果: 7 True 8 True 9 False10 True
View Code

 4、=、==、is 、id()、小数据池等小知识点

= : 赋值;

== : 比较值是否相等;

is :比较的是内存地址;

id():求括号里的内容;

1 li2 =  [1,2,3,4]2 li3 = li23 print(id(li2),id(li3))4 li2 = [1,2,3,4]5 print(id(li2),id(li3))6 7 结果:8 43657416 436574169 43508680 43657416
View Code#数字、字符串的小数据池:就是为了一定程度范围内节省内存空间
#数字、字符串的小数据池:就是为了一定程度范围内节省内存空间 #数据的范围:-5-----256 #字符串的范围:1, 不能有特殊字符(表示怀疑这一说法)
2、 s *20,不能超过20个字符; # 对于list dict tuple set不具备小数据池概念
1 li1 = 52 li2 = 53 print(id(li1),id(li2))4 结果:5 1473732896 1473732896
View Code
1 li3 = 'peng' 2 li4 = 'peng' 3 print(li3 is li4) 4  5 li5 = 'peng@++=' 6 li6 = 'peng@++=' 7 print(li5 is li6) 8  9 s1 = 'a'*2010 s2 = 'a'*2011 print(s1 is s2)12 13 s1 = 'a'*2114 s2 = 'a'*2115 print(s1 is s2)16 结果:17 True18 True19 True20 False
View Code

 

 

转载于:https://www.cnblogs.com/xiaofei1106/p/10520340.html

你可能感兴趣的文章
Charles抓取https请求
查看>>
LAMP环境搭建
查看>>
C语言的变量的内存分配
查看>>
clientcontainerThrift Types
查看>>
链接全局变量再说BSS段的清理
查看>>
hdu 1728 逃离迷宫
查看>>
HTML5与CSS3权威指南之CSS3学习记录
查看>>
docker安装部署
查看>>
AVL树、splay树(伸展树)和红黑树比较
查看>>
多媒体音量条显示异常跳动
查看>>
运算符及题目(2017.1.8)
查看>>
React接入Sentry.js
查看>>
ssh自动分发密匙脚本样板
查看>>
转 小辉_Ray CORS(跨域资源共享)
查看>>
Linux安装postgresql
查看>>
MyBatis启动:MapperStatement创建
查看>>
【 全干货 】5 分钟带你看懂 Docker !
查看>>
[转]优化Flash性能
查看>>
popStar手机游戏机机对战程序
查看>>
Java Web项目结构
查看>>