python:第十七章:布尔运算符(逻辑运算符)
一,布尔运算符有哪些?
and
运算是与运算,只有两个值都为True
,and
运算结果才是True
,如下表
a | b | a and b |
---|---|---|
True | True | True |
True | False | False |
False | True | False |
False | False | False |
or
运算是或运算,只要其中有一个值为True
,or
运算结果就是True
a | b | a or b |
---|---|---|
True | True | True |
True | False | True |
False | True | True |
False | False | False |
not
运算是非运算,它是一个单目运算符,把True
变成False
,False
变成True
a | not a |
---|---|
True | False |
False | True |
例子:and运算符
1
2
3
4
5
6
|
# and运算 print ( True and True ) # True print ( True and False ) # False print ( False and False ) # False print ( 8 > 3 and 5 > 1 ) # True print ( 4 > 7 and 5 > 1 ) # False |
运行结果:
True
False
False
True
False
例子:or运算符
1
2
3
4
5
6
|
# or运算 print ( True or True ) # True print ( True or False ) # True print ( False or False ) # False print ( 8 > 3 or 2 > 5 ) # True print ( 4 > 7 or 5 > 9 ) # False |
运行结果:
True
True
False
True
False
例子:not运算符
1
2
3
4
5
|
# not运算 print ( not True ) # False print ( not False ) # True print ( not 1 > 3 ) # True print ( not 3 > 1 ) # False |
运行结果:
False
True
True
False
说明:刘宏缔的架构森林—专注it技术的博客,
网站:https://blog.imgtouch.com
原文: https://blog.imgtouch.com/index.php/2023/11/14/python-di-shi-qi-zhang-bu-er-yun-suan-fu/
代码: https://github.com/liuhongdi/ 或 https://gitee.com/liuhongdi
说明:作者:刘宏缔 邮箱: 371125307@qq.com
二,优先级
运算符 | 优先级 |
---|---|
not | 高 |
and | 中 |
or | 低 |
按照优先顺序做逻辑运算时,为了清晰可以加上括号
表达式 | 等价表达式 |
---|---|
a or b and c | a or (b and c) |
a and b or c and d | (a and b) or (c and d) |
a and b and c or d | ((a and b) and c) or d |
not a and b or c | ((not a) and b) or c |
分类:
python基础课程
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
2021-11-18 vue.js 3.2.22:菜单每行依次滑动出现的动画
2020-11-18 linux(ubuntu20.10):用apt方式安装php7.4.9
2020-11-18 从vue-cli2.x升级到@vue/cli 4.x(node v14.15.0 / vue.js 3.0)