Python3逻辑运算符与成员运算符
作者:AYO小涛 时间:2021-03-29 18:59:14
一·逻辑运算符
优先级:not>and>or
符号 | 名称 | 描述 |
and | 逻辑与运算 | 当and的两边同时满足条件时,结果为True,否则就为False |
or | 逻辑或运算 | 当or的两边只要有一边满足条件,结果就为True,只有两边均不满足条件时才为False |
not | 逻辑非运算 | 如果是True则输出False,False就输出True |
注意:在 Python 中,and 和 or 不一定会计算右边表达式的值,有时候只计算左边表达式的值就能得到最终结果。
and 和 or 运算符会将其中一个表达式的值作为最终结果,而不一定就是将 True 或者 False 作为最终结果(也有可能不是bool)。
二·成员运算符
符号 | 描述 |
in | 判断什么在什么里面,在的话返回True,不在就返回False |
not in | 判断什么不在什么里面,不在就返回True,在就返回False |
三·代码
1·and
print(1+1 == 2 and 2+2 == 4)
# 输出结果:True
print(1+1 == 1 and 2+2 == 4 and 3+3 == 5)
# 输出结果:
2·or
print(1+1 == 1 or 2+2 == 4 or 3+3 == 5)
# 输出结果:True
print(1+1 == 1 or 2+2 == 5)
# 输出结果:False
3·not
print(not True)
# 输出结果:False
print(not 1+1 == 3)
# 输出结果:True
4·in
a = "a"
b = "abc"
print(a in b)
# 输出结果:True
5·not in
a = "a"
b = "abc"
print(a not in b)
# 输出结果:False
来源:https://blog.51cto.com/u_15320761/5649858
标签:Python,逻辑,成员,运算符
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python OpenCV之常用滤波器使用详解
2023-02-07 22:18:25
![](https://img.aspxhome.com/file/2023/9/81589_0s.jpg)
解决Pytorch在测试与训练过程中的验证结果不一致问题
2022-08-18 03:50:13
注册表单之死
2008-08-07 13:02:00
![](https://img.aspxhome.com/file/UploadPic/20088/7/20088713716694s.gif)
Python实现微博动态图片爬取详解
2023-09-14 05:47:25
![](https://img.aspxhome.com/file/2023/0/61730_0s.jpg)
一行Python代码制作动态二维码的实现
2023-11-04 02:32:19
![](https://img.aspxhome.com/file/2023/7/78847_0s.jpg)
PyQt5实现暗黑风格的计时器
2023-07-07 02:49:30
![](https://img.aspxhome.com/file/2023/0/61990_0s.jpg)
thinkphp5实用入门进阶知识点和各种常用功能代码汇总
2023-05-25 02:48:34
python 通过exifread读取照片信息
2022-12-09 13:44:53
用PHP编写每周签到功能以提高用户参与度
2023-05-27 17:24:54
浅析python中5个带key的内置函数
2021-08-27 00:31:25
处理Python中的URLError异常的方法
2021-06-04 03:33:15
Bresenham图形算法JavaScript版本
2010-01-25 12:09:00
30个最常用css选择器解析
2011-06-16 20:36:37
互联网产品交互事件分析
2009-10-06 15:23:00
Webform 内置对象 Session对象、Application全局对象,ViewState详细介绍
2023-06-30 02:14:07
10个Python面试常问的问题(小结)
2023-04-11 19:36:15
JavaScript实现简单贪吃蛇效果
2023-08-13 05:48:08
![](https://img.aspxhome.com/file/2023/6/56036_0s.gif)
ASP连接MySQL数据库的方法
2010-03-14 11:25:00
如何利用Image Data Type在主页中显示图形?
2010-01-01 15:13:00
python 重定向获取真实url的方法
2022-10-25 03:24:46