Python逻辑操作符
来源:互联网 发布:对接淘宝库存管理软件 编辑:程序博客网 时间:2024/05/17 07:37
刚开始看python,觉得这个逻辑操作符和其他语言有些区别,所以就记录下来。
Python的逻辑操作有三种:and、or、not。分别对应与、或、非。
严格的说,逻辑操作符的操作数应该为布尔表达式。但Python对此处理的比较灵活。
即使操作数是数字,解释器也把他们当成“表达式”。
非0的数字的布尔值为1,0的布尔值为0.
在Python中,空字符串为假,非空字符串为真。非零的数为真。
对于and操作符a and b:
只要左边的表达式为真,整个表达式返回的值是右边表达式的值,否则,返回左边表达式的值
对于or操作符 a or b:
只要左边的表达式为真,整个表达式返回的值是左边表达式的值,否则,返回右边表达式的值
对于not操作符 not a:
如果 a 为 True,返回 False 。如果 a 为 False,它返回 True。
举例:
假设变量 a 为 10, b为 20
(a and b) 返回 20。
(a or b) 返回 10。
not(a and b) 返回 False
#coding:utf-8 test1 = 12 test2 = 0 test3 = '' test4 = "First" print test1 and test3 #result = '' print test3 and test1 #result = '' print test1 and test4 #result = "First" print test4 and test1 #result = 12 print test1 or test2 #result = 12 print test1 or test3 #result = 12 print test3 or test4 #result = "First" print test2 or test4 #result = "First" print test1 or test4 #result = 12 print test4 or test1 #result = "First" print test2 or test3 #result = '' print test3 or test2 #result = 0
0 0
- Python逻辑操作符
- Python学习笔记三:逻辑操作符
- python基础之逻辑操作符
- 5.python 算术操作符逻辑操作符
- 【python】算术、比较、逻辑操作符及他们的优先级
- PHP逻辑操作符
- 逻辑操作符
- MongoDB_逻辑操作符
- ORACLE逻辑操作符
- mysql逻辑操作符
- 逻辑操作符
- 逻辑或操作符
- 逻辑非操作符
- day04 逻辑操作符& | ^
- Lua逻辑操作符
- 【操作符】 - 逻辑操作符
- javascript中的逻辑操作符
- javascript中的逻辑操作符
- HDFS存储原理
- Tensorflow安装及基本使用
- SVN提示:由于目标机器积极拒绝,无法连接 的解决方法
- gdb
- Java多线程
- Python逻辑操作符
- Android 开机动画制作
- NOIP2016普及组复赛全国一等奖名单及排名(续表_919~1419名)
- 初始化一个时间data = 2016-12-13,通过函数计算出以下值:
- myeclipse配置maven
- 函数重载、覆盖与隐藏
- MySQL分区表详解
- 关于Bitmap使用的笔记汇总
- RecyclerView 自适应高度 正确做法