Truth Value Testing (Python)

Advertisement

Any object can be tested for truth value, for use in an if or while condition or as operand of the Boolean operations below. The following values are considered false:

  • None
  • False
  • zero of any numeric type, for example, 0, 0L, 0.0, 0j.
  • any empty sequence, for example, '', (), [].
  • any empty mapping, for example, {}.
  • instances of user-defined classes, if the class defines a __nonzero__() or __len__() method, when that method returns the integer zero or bool valueFalse. [1]

All other values are considered true — so objects of many types are always true.

Operations and built-in functions that have a Boolean result always return 0 or False for false and 1 or True for true, unless otherwise stated. (Important exception: the Boolean operations or and and always return one of their operands.)

转自:

http://docs.python.org/2/library/stdtypes.html#truth-value-testing

Similar Posts:

  • Python built-in types - Truth Value Testing

    Python内建类型之真值检测(Truth Value Testing) Manual Any object can be tested for truth value, for use in an if or while condition or as operand of the Boolean operations below. The following values are considered false: None False zero of any numeric type, f

  • python开发_python中的Boolean运算和真假值

    python中的真假值: Truth Value Testing Any object can be tested for truth value, for use in an if or while condition or as operand of the Boolean operations below. The following values are considered false: 1.None 2.False 3.zero of any numeric type, for ex

  • Python : Data model

    3. Data model 3.1. Objects, values and types¶ Objects are Python's abstraction for data. All data in a Python program is represented by objects or by relations between objects. (In a sense, and in conformance to Von Neumann's model of a "stored progr

  • python模块—socket

    创建套接字: socket.socket(family,type[,protocal]) 使用给定的地址族.套接字类型.协议编号(默认为0)来创建套接字. 1. socket类型: socket.AF_UNIX:只能够用于单一的Unix系统进程间通信 socket.AF_INET:服务器之间网络通信 socket.AF_INET6:IPv6 socket.SOCK_STREAM:流式socket,for TCP socket.SOCK_DGRAM:数据报式socket,for UDP socke

  • numpy教程:逻辑函数Logic functions

    http://blog.csdn.net/pipisorry/article/details/48208433 真值测试Truth value testing all(a[, axis, out, keepdims]) Test whether all array elements along a given axis evaluate to True. any(a[, axis, out, keepdims]) Test whether any array element along a gi

  • Python3基础-语言内置

    Python3基础-语言内置 节选自Python文档的Library Reference部分,位置: Python » 3.4.3 Documentation » The Python Standard Library » Built-in Functions 内置函数 部分例如: 函数 例子 bin() bin(5) => '0b101' chr() chr(27721) => '汉' dict() eval() x=3, eval('x+1') => 4 hex() id() int

  • Automatic IE Testing With Python

    记在了我的英文博客中,简要介绍了如何使用python自动化IE操作,以及可能遇到的问题. http://sharpscottqian.blogspot.com/2012/08/automatic-ie-testing-with-python.html

  • python内建函数(系列原创)(翻译)

    2.1 Built-in Functions 注:本人翻译完全是为了自学,网上也许有翻译稿,但我觉得能把英文技术文档恰当地翻译出来(或者说用自己的话表达出来)才能真正领会很多知识! 英文原文档为ActivePython 2.4 Documentation上的(序号如上). 由于本人水平有限,有不准确的地方,请各位同好朋友批评指正!谢谢! 我把英文全放在上面,一段一段对照译的! 翻译正文如下: Python Library Reference Previous: 2. Built-In Objec

  • Python __Underscore__ Methods

    1   Intro My intention for this article is to be a quick-reference guide: all the information you might need about a method or property condensed into a few lines. This might not be possible for something like __slots__, perhaps we should link to the

  • A collection of not-so-obvious Python stuff you should know

    Sebastian Raschka last updated: 04/25/2014 Link to this IPython Notebook on GitHub All code was executed in Python 3.4 A collection of not-so-obvious Python stuff you should know! I am really looking forward to your comments and suggestions to improv

Tags: