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. jekens介绍及服务搭建

    https://blog.csdn.net/achuo/article/details/51086599 https://blog.csdn.net/qq_37372007/article/detai ...

  2. Python全栈工程师(元组、字典)

     ParisGabriel     感谢 大家的支持  你们的阅读评价就是我最好的更新动力  我会坚持吧排版做的越来越好        每天坚持 一天一篇 点个订阅吧  灰常感谢    当个死粉也阔以 ...

  3. php中普通方法和静态方法的区别以及抽象类和接口

    实例化类产生对象.class fenbi{ //普通成员,属于对象 public $length = "10cm"; //静态成员,静态变量,属于类. public static ...

  4. nyoj 题目49 开心的小明

    开心的小明 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 小明今天很开心,家里购置的新房就要领钥匙了,新房里有一间他自己专用的很宽敞的房间.更让他高兴的是,妈妈昨天 ...

  5. 【bzoj2212】[Poi2011]Tree Rotations 权值线段树合并

    原文地址:http://www.cnblogs.com/GXZlegend/p/6826614.html 题目描述 Byteasar the gardener is growing a rare tr ...

  6. [2018-9-4T2]探索黑暗dark

    题目大意:有一棵树,第$i$个点的点权为$s_i(s_1>0)$.初始有每个点都是亮的.$m$次修改,每次改变一个点的亮暗,回答包含$1$的全亮的连通块中点权和最大的连通块的和的值. 题解:正解 ...

  7. [AGC005D] ~K Perm Counting [dp]

    题面 传送门 思路 首先可以明确的一点是,本题中出现不满足条件的所有的数,都是分组的 只有模$K$意义下相同的数之间才会出现不满足条件的情况,而且仅出现在相邻的情况 那么我们考虑把这个性质利用起来 我 ...

  8. 在浏览器中进行深度学习:TensorFlow.js (八)生成对抗网络 (GAN

    Generative Adversarial Network 是深度学习中非常有趣的一种方法.GAN最早源自Ian Goodfellow的这篇论文.LeCun对GAN给出了极高的评价: “There ...

  9. 多校4 lazy running (最短路)

    lazy running(最短路) 题意: 一个环上有四个点,从点2出发回到起点,走过的距离不小于K的最短距离是多少 \(K <= 10^{18} 1 <= d <= 30000\) ...

  10. spring in action 学习十二:property placeholder 注解的方式实现避免注入外部属性硬代码化

    这里的注解是指@PropertySource这个注解.用@PropertySource这个注解加载.properties文件. 案例的目录结构如下: student.properties的代码如下: ...