Python_Tips[4] -> and 和 or 的计算原则
and和or / and & or
对于and和or,可以连接多个值,其分别遵循原则:
- 全是 And: 返回第一个遇到的无效值,若全有效,返回最后一个有效值
- 全是 Or: 返回第一个遇到的有效值,若全无效,返回最后一个无效值
- 混用 And & Or: 优先对所有 and 进行计算,随后计算所有 or,计算规则同上。
print(1 and 0 and 2) #
print(1 and 2 and 3) #
print(0 or 1 or 0) #
print(0 or '' or None) # None
# Equal to (1 and 0) or (3 and 2 and 4) or (None and 5)
print(1 and 0 or 3 and 2 and 4 or None and 5) #
可以利用这些特性进行一些狡猾的处理,例如print出有效内容,内容无效则显示自定义的字符串。
可参考下面的代码
"""
The return of 'and' and 'or'
As for 'or', it will return the first valid one, if all invalid, return the last invalid one
As for 'and', it will return the first invalid one, if all valid, return the last valid one
"""
class Foo(): pass def foo(): pass valid = [7, True, (7,), [7], {'': 7}, foo, Foo]
invalid = [0, None, False, '', (), [], {}] # 'or': Invalid or Valid --> return the first valid one
print('----Below is Invalid or Valid----')
for iv in invalid:
print(iv or 'Valid' or 'Real') # 'or': Invalid or Invalid --> return the last invalid one
print('----Below is Invalid or Invalid----')
for iv in invalid:
print(iv or None or False) # 'or': Valid or Valid --> return the first valid one
print('----Below is Valid or Valid----')
for vl in valid:
print(vl or 'Valid') # 'and': Invalid and Valid --> return the first invalid one
print('----Below is Invalid and Valid----')
for iv in invalid:
print(iv and 'Valid' and 'Real') # 'and': Valid and Valid --> return the last valid one
print('----Below is Valid and Valid----')
for vl in valid:
print(vl and 7 and True) # 'and': Valid or Invalid --> return the first invalid one
print('----Below is Valid and Invalid----')
for vl in valid:
print(vl and None and False) print('----Below is tricky print----')
# We can use it to make a tricky print
for iv in invalid:
print('I have %s' % (iv or 'None'))
for vl in valid:
print('I have %s' % (vl or 'None'))
和输出结果
----Below is Invalid or Valid----
Valid
Valid
Valid
Valid
Valid
Valid
Valid
----Below is Invalid or Invalid----
False
False
False
False
False
False
False
----Below is Valid or Valid----
7
True
(7,)
[7]
{'': 7}
<function foo at 0x02F7F930>
<class '__main__.Foo'>
----Below is Invalid and Valid----
0
None
False ()
[]
{}
----Below is Valid and Valid----
True
True
True
True
True
True
True
----Below is Valid and Invalid----
None
None
None
None
None
None
None
----Below is tricky print----
I have None
I have None
I have None
I have None
I have None
I have None
I have None
I have 7
I have True
I have 7
I have [7]
I have {'': 7}
I have <function foo at 0x02F7F930>
I have <class '__main__.Foo'>
Python_Tips[4] -> and 和 or 的计算原则的更多相关文章
- haier周的计算原则
现使用oracle的sql表示出haier周, 经过对其生成结果的分析,发现海尔周是以周日到周六分别作为一周的始末, 用到的oracle sql中会涉及到calendar week的定义,还涉及到了I ...
- </2017><2018>
>>> Blog 随笔原始文档及源代码 -> github: https://github.com/StackLike/Python_Note >>> 统计信 ...
- access-list/eigrp等 反掩码计算
access-list/eigrp等 反掩码计算 原则:地址部分,相同的照写,不同的写"0" 反掩码部分,相同的写"0",不同的写"1&quo ...
- 【2019云栖大会】这一场,我们好好聊聊5G和边缘计算
一年一度的科技盛会杭州云栖大会Apsara Conference就要来了9月25-27日数万名开发者将齐聚杭州云栖小镇共同探索人类科技演进的脉搏聚焦面向未来的创新.热点技术话题 5G和边缘计算是201 ...
- 阿里云杨敬宇:5G时代,边缘计算将发挥更大价值
“5G时代,边缘计算将发挥更大价值.”3月8日,阿里云边缘计算技术负责人杨敬宇向媒体表示,边缘计算作为5G时代的一项关键技术,未来将成为不可或缺的基础设施之一. 5G时代万物智联将真正成为现实,但对计 ...
- 从备考PMP到与项目经理同呼吸
前言 PMP是什么梗? 项目管理专业人士资格认证.它是由美国项目管理协会(Project Management Institute(PMI)发起的,严格评估项目管理人员知识技能是否具有高品质的资格认证 ...
- Hadoop学习笔记—4.初识MapReduce
一.神马是高大上的MapReduce MapReduce是Google的一项重要技术,它首先是一个编程模型,用以进行大数据量的计算.对于大数据量的计算,通常采用的处理手法就是并行计算.但对许多开发者来 ...
- [Hadoop] Hadoop学习笔记之Hadoop基础
1 Hadoop是什么? Google公司发表了两篇论文:一篇论文是“The Google File System”,介绍如何实现分布式地存储海量数据:另一篇论文是“Mapreduce:Simplif ...
- JS逻辑运算符&&与||的短路运算
最近看到一个360面试题,题目如下: 下面代码的输出值是? alert(1&&2); 正确的结果是 2. 1.后来仔细研究了一下JS逻辑运算的相关内容,在MDN上面找到相应描述: 下面 ...
随机推荐
- 手把手教你写Windows 64位平台调试器
本文网页排版有些差,已上传了doc,可以下载阅读.本文中的所有代码已打包,下载地址在此. ------------------------------------------------------- ...
- .gitignore 中文文件夹无效
有个文件夹名如:测试 在.gitignore中添加 /测试/ 但运行命令git status后发现还是被追踪到了 一番搜索后终于发现.gitignore文件编码是GBK的,重新将文件保存成utf ...
- Python利器一之requests
Python利器一之requests 一.教程涉及开发语言.脚本.框架.数据库等内容 Python + requests 通过 pip 安装: pip install requests 通过 easy ...
- Python处理Sqlite3数据库
sqlite3比较小众 本章主要通过Python Code表述如何增.查.改.删 sqlite3 DB 一.直接上代码 #!/usr/bin/env python # -*- coding: utf- ...
- 生成器 yield, next ,send
重要的yield :相当于一个断层,我们再用next取拿出每一层重要的next :生成器查看装置,查看每一个断层重要的send :和next一样查看每一个段层,不过在查看第二个断层的时候,就可以对前面 ...
- Convert.ToBase64String(Byte[])和Encoding.UTF8.GetString(Byte[])的区别
Encoding.UTF8.GetString是针对使用utf8编码得到的字符串对应的byte[]使用,可以还原我们能看懂的字符串而Convert.ToBase64String是对任意byte[]都可 ...
- NGUI-UIProgressBar,UIScrollBar,UISlider
UIProgressBar是UIScrollBar和UISlider的基类 1.先来看下UIProgressBar(进度条)的使用 层次: progressBar的Inspector视图: 而fore ...
- [18/12/3]蓝桥杯 练习系统 入门级别 Fibonacci数列求模问题 题解思路
前言略. 看到这个题目本来应该很高兴的,因为什么,因为太TM的基础了啊! 可是当你用常规方法尝试提交OJ时你会发现..hhh...运行超时..(开心地摇起了呆毛 //Fibonacci数列递归一般问题 ...
- shell之正则表达式
正则表达式(regular expression ,REGEXP): 元字符: .:匹配任意单个字符 []:匹配指定范围内的任意单个字符 [^]:匹配指定范围外的任意字符 字符集合:使用[字符集合] ...
- 爬虫:Scrapy9 - Feed exports
实现爬虫时最经常提到的需求就是能合适的保存爬取到的数据,或者说,生成一个带有爬取数据的“输出文件”(通常叫“输出 feed”),来供其它系统使用. Scrapy 自带了 Feed 输出,并且支持多种序 ...