andor / and & or


对于and和or,可以连接多个值,其分别遵循原则:

  1. 全是 And: 返回第一个遇到的无效值,若全有效,返回最后一个有效值
  2. 全是 Or: 返回第一个遇到的有效值,若全无效,返回最后一个无效值
  3. 混用 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 的计算原则的更多相关文章

  1. haier周的计算原则

    现使用oracle的sql表示出haier周, 经过对其生成结果的分析,发现海尔周是以周日到周六分别作为一周的始末, 用到的oracle sql中会涉及到calendar week的定义,还涉及到了I ...

  2. </2017><2018>

    >>> Blog 随笔原始文档及源代码 -> github: https://github.com/StackLike/Python_Note >>> 统计信 ...

  3. access-list/eigrp等 反掩码计算

    access-list/eigrp等 反掩码计算 原则:地址部分,相同的照写,不同的写"0"     反掩码部分,相同的写"0",不同的写"1&quo ...

  4. 【2019云栖大会】这一场,我们好好聊聊5G和边缘计算

    一年一度的科技盛会杭州云栖大会Apsara Conference就要来了9月25-27日数万名开发者将齐聚杭州云栖小镇共同探索人类科技演进的脉搏聚焦面向未来的创新.热点技术话题 5G和边缘计算是201 ...

  5. 阿里云杨敬宇:5G时代,边缘计算将发挥更大价值

    “5G时代,边缘计算将发挥更大价值.”3月8日,阿里云边缘计算技术负责人杨敬宇向媒体表示,边缘计算作为5G时代的一项关键技术,未来将成为不可或缺的基础设施之一. 5G时代万物智联将真正成为现实,但对计 ...

  6. 从备考PMP到与项目经理同呼吸

    前言 PMP是什么梗? 项目管理专业人士资格认证.它是由美国项目管理协会(Project Management Institute(PMI)发起的,严格评估项目管理人员知识技能是否具有高品质的资格认证 ...

  7. Hadoop学习笔记—4.初识MapReduce

    一.神马是高大上的MapReduce MapReduce是Google的一项重要技术,它首先是一个编程模型,用以进行大数据量的计算.对于大数据量的计算,通常采用的处理手法就是并行计算.但对许多开发者来 ...

  8. [Hadoop] Hadoop学习笔记之Hadoop基础

    1 Hadoop是什么? Google公司发表了两篇论文:一篇论文是“The Google File System”,介绍如何实现分布式地存储海量数据:另一篇论文是“Mapreduce:Simplif ...

  9. JS逻辑运算符&&与||的短路运算

    最近看到一个360面试题,题目如下: 下面代码的输出值是? alert(1&&2); 正确的结果是 2. 1.后来仔细研究了一下JS逻辑运算的相关内容,在MDN上面找到相应描述: 下面 ...

随机推荐

  1. unity3d中C#与JS的一些区别

    unity3d目前支持C#和JS两种脚本语言. 学习过程中发现很多教程使用的是JS语言,自己还是用C#比较多,unity原生使用的是Mono,使用C#会更加接近unity的编程思想. 1.方法的定义, ...

  2. Python 爬虫-豆瓣读书

    import requests from bs4 import BeautifulSoup def parse_html(num): headers = { 'User-Agent': 'Mozill ...

  3. eclipse集成python(Pydev插件安装)

    1.下载PyDev的压缩包,解压后会有features和plugins两个文件夹,将两个文件夹的内容拷贝到eclipse对应的文件夹中,重新启动eclipse 2.配置python 2.1打开ecli ...

  4. python /usr/bin/python^M: bad interpreter: No such file

    今天在WingIDE下写了个脚本,传到服务器执行后提示: -bash: /usr/bin/autocrorder: /usr/bin/python^M: bad interpreter: No suc ...

  5. Elasticsearch同义词词汇单元过滤器

    1 简单扩展 "jump,hop,leap" 搜索jump会检索出包含jump.hop或leap的词 1.1 扩展应用在索引阶段 1.2 扩展应用在查询阶段 1.3 对比 2 简单 ...

  6. 团队项目-第一次Scrum 会议

    时间:10.23 时长:30分钟 地点:F楼2层沙发休息处 工作情况 团队成员 已完成任务 待完成任务 解小锐 学习使用cocos creator 学习官方样例 陈鑫 学习JavaScript 学习c ...

  7. Android记事本05

    昨天: intentFilter 今天: URL和logcat 问题: ADK更新后无法打开布局文件.xml

  8. struts框架搭建

    struts是开源框架.使用Struts的目的是为了帮助我们减少在运用MVC设计模型来开发Web应用的时间.如果我们想混合使用Servlets和JSP的优点来建立可扩展的应用,struts是一个不错的 ...

  9. nf_register_hooks解读

    nf_register_hooks是什么 net->netns_nf->nf_hook_entry[NFPROTO_NUMPROTO][NF_MAX_HOOKS=8] (nf)       ...

  10. [poj] 3057 Evacuation

    原题 题目大意 墙壁"X",空区域(都是人)".", 门"D". 人向门移动通过时视为逃脱,门每秒能出去一个人,人可以上下左右移动,墙阻止移 ...