`

Python中的and和or

 
阅读更多

ython中的and和or

4.6. and 和 or 的特殊性质
在Python 中,and 和 or 执行布尔逻辑演算,如你所期待的一样,但是它们并不返回布尔值;而是,返回它们实际进行比较的值之一。


例 4.15. and 介绍
>>> 'a' and 'b'        

'b'

>>> '' and 'b'         

''

>>> 'a' and 'b' and 'c'

'c'


使用 and 时,在布尔上下文中从左到右演算表达式的值。0、''、[]、()、{}、None 在布尔上下文中为假;其它任何东西都为真。还好,几乎是所有东西。默认情况下,布尔上下文中的类实例为真,但是你可以在类中定义特定的方法使得类实例的演算值为假。你将会在第 5 章中了解到类和这些特殊方法。如果布尔上下文中的所有值都为真,那么 and 返回最后一个值。在这个例子中,and 演算 'a' 的值为真,然后是 'b' 的演算值为真,最终返回 'b'。 
如果布尔上下文中的某个值为假,则 and 返回第一个假值。在这个例子中,'' 是第一个假值。 
所有值都为真,所以 and 返回最后一个真值,'c'。

 

 

 

>>> 'a' or 'b'         

'a'

>>> '' or 'b'          

'b'

>>> '' or [] or {}     

{}

>>> def sidefx():

...     print "in sidefx()"

...     return 1

>>> 'a' or sidefx()    

'a'
使用 or 时,在布尔上下文中从左到右演算值,就像 and 一样。如果有一个值为真,or 立刻返回该值。本例中,'a' 是第一个真值。 
or 演算 '' 的值为假,然后演算 'b' 的值为真,于是返回 'b' 。 
如果所有的值都为假,or 返回最后一个假值。or 演算 '' 的值为假,然后演算 [] 的值为假,依次演算 {} 的值为假,最终返回 {} 。 
注意 or 在布尔上下文中会一直进行表达式演算直到找到第一个真值,然后就会忽略剩余的比较值。如果某些值具有副作用,这种特性就非常重要了。在这里,函数 sidefx 永远都不会被调用,因为 or 演算 'a' 的值为真,所以紧接着就立刻返回 'a' 了。

分享到:
评论

相关推荐

    通过实例解析python and和or使用方法

    在python中and和or返回的值并不是True和false这么简单。虽然他们看上去和c++中的&&和||有些相似。在了解and和or之前,我们先要了解python中的True和False。 在python里面,0、”、[]、()、{}、None为假,其它任何...

    Python中and和or如何使用

    在Python 中,and 和 or 执行布尔逻辑演算,如你所期待的一样,但是它们并不返回布尔值;而是,返回它们实际进行比较的值之一。 代码如下: >>> 'a' and 'b' 'b' >>> '' and 'b' '' >>> 'a' and 'b' and 'c' 'c' 在...

    python关键字and和or用法实例

    python 中的and从左到右计算表达式,若所有值均为真,则返回最后一个值,若存在假,返回第一个假值。 or也是从左到有计算表达式,返回第一个为真的值。 复制代码 代码如下: IDLE 1.2.4 >>>’a’and’b’ ‘b’ >>>”...

    Python中AND、OR的一个使用小技巧

    python中的and-or可以用来当作c用的?:用法。... 您可能感兴趣的文章:python and or用法详解python关键字and和or用法实例Python and、or以及and-or语法总结浅谈Python中(&,|)和(and,or)之间的区别

    Python中and 与or的用法.docx

    python等级考试料1-5级综合学习资料python等级考试料1-5级综合学习资料python等级考试料1-5级综合学习资料python等级考试料1-5级综合学习资料python等级考试料1-5级综合学习资料python等级考试料1-5级综合学习资料

    python中逻辑与或(and、or)和按位与或异或(&、|、^)区别

    按位与或(&、|、^):按照二进制进行逻辑运算 ...一:逻辑与或 (and、or) 1:and :从左到右运行,运行到表达式不正确就输出False,后面的表达式逻辑短路;若运行到函数且无返回值,则输出None,且后面的表达式逻

    Python-DockerfilesforPython27orPython36andSeleniuminheadlessChromeorFirefox

    Dockerfiles for Python 2.7 or Python 3.6 and Selenium in headless Chrome or Firefox

    Python and、or以及and-or语法总结

    主要介绍了Python and、or以及and-or语法总结,本文分别给出实例讲解它们的使用方法,需要的朋友可以参考下

    python and or用法详解

    and 和 or 是python的两个逻辑运算符,可以使用and , or来进行多个条件内容的判断。下面通过代码简单说明下and or的用法: 1. or:当有一个条件为真时,该条件即为真。逻辑图如下: 测试代码如下: a=raw_input('...

    Data Visualization with Python and JavaScript.azw3

    If you’re ready to create your own web-based data visualizations—and know either Python or JavaScript— this is the book for you. Learn how to manipulate data with Python Understand the ...

    Data Visualization with Python and JavaScript

    , Get data programmatically, using scraping tools or web APIs, Clean and process data using Python's heavyweight data-processing libraries, Deliver data to a browser using a lightweight Python server ...

    [Python.and.HDF5(2013.10)].Andrew.Collette.文字版.pdf

    Over the past several years, Python has emerged as a credible alternative to scientific analysis environments like IDL or MATLAB. Stable core packages now exist for han‐ dling numerical arrays (NumPy...

    Python 中文手册

    Abstract Python is an easy to learn, powerful programming language. It has efficient high-level data structures and a ...模块和程序,接下来可以从Python 库参考手册中进一步学习Python复杂多变的库和模块。

    Black Hat PytHon Python Programming for Hackers and Pentesters

    requires rapid Python tool development, with a focus on execution and delivering results (not necessarily on prettiness, optimization, or even stability). Throughout this book you will learn that this...

    Rapid GUI Programming with Python and Qt

    介绍了使用python 和pyqt 的GUI编程。 This book teaches how to write GUI applications using the Python programming language and the Qt application development framework. The only prior knowledge ...

    Beginning Game Development with Python and Pygame

    <br>Beginning Game Development with Python and Pygame is written with the budding game developer in mind, introducing games development through the Python programming language and the popular ...

    Data Structures and Algorithms in Python

    This book is based upon the book Data Structures and Algorithms in Java by Goodrich and Tamassia, and the related Data Structures and Algorithms in C++ by Goodrich, Tamassia, and Mount. However, this ...

Global site tag (gtag.js) - Google Analytics