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 example, 0, 0.0, 0j.

4.any empty sequence, for example, '', (), [].

5.any empty mapping, for example, {}.

6.instances of user-defined classes, if the class defines a __bool__() or __len__() method, when that method returns the integer zero or bool value False. [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.)

总结:

#在python中,任何对象都可以判断其真假值:True,False
#在if或while条件判断中,下面的情况值为False:
#
# 1.None
# 2.Flase
# 3.数值为0的情况,如:0,0.0,0j
# 4.所有空序列,如:'',(),[]
# 5.所有空mapping,如:{}
# 6.instances of user-defined classes, if the class defines a __bool__() or __len__() method,
# when that method returns the integer zero or bool value False.
#
# All other values are considered true — so objects of many types are always true.
#
#
# 在运算操作和内建函数返回Boolean结果0或者Flase表示false
#1或True表示true
#

python中的Boolean运算如下:

Boolean Operations

 print('x or y -> if x is false,then y, else x ')
x, y = 2, 0
print('{} or {} = {}'.format(x, y, x or y))
x1, y1 = 0, 10
print('{} or {} = {}'.format(x1, y1, x1 or y1))
x2, y2 = 0, 0
print('{} or {} = {}'.format(x2, y2, x2 or y2)) print('#' * 50)
print('x and y -> if x is false,then x, else y ')
print('{} and {} = {}'.format(x, y, x and y))
x1, y1 = 0, 10
print('{} and {} = {}'.format(x1, y1, x1 and y1))
x2, y2 = 0, 0
print('{} and {} = {}'.format(x2, y2, x2 and y2)) print('#' * 50)
print('not x -> if x is false,then True,else False ')
x = 2
print('not {} = {}'.format(x, not x))
x = 0
print('not {} = {}'.format(x, not x))

运行效果:

Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
x or y -> if x is false,then y, else x
2 or 0 = 2
0 or 10 = 10
0 or 0 = 0
##################################################
x and y -> if x is false,then x, else y
2 and 0 = 0
0 and 10 = 0
0 and 0 = 0
##################################################
not x -> if x is false,then True,else False
not 2 = False
not 0 = True
>>>

python开发_python中的Boolean运算和真假值的更多相关文章

  1. python开发_python中字符串string操作

    在python中,对于字符串string的操作,我们有必要了解一下,这样在我们的以后的开发中会给我们带来很多方便 下面是我学习的笔记: #python-string #python中的字符串用单引号' ...

  2. python开发_python中str.format()

    格式化一个字符串的输出结果,我们在很多地方都可以看到,如:c/c++中都有见过 下面看看python中的字符串格式函数str.format(): 1 #使用str.format()函数 2 3 #使用 ...

  3. python开发_python中的range()函数

    python中的range()函数的功能hen强大,所以我觉得很有必要和大家分享一下 就好像其API中所描述的: If you do need to iterate over a sequence o ...

  4. python开发_python中的module

    在python中,我们可以把一些功能模块化,就有一点类似于java中,把一些功能相关或者相同的代码放到一起,这样我们需要用的时候,就可以直接调用了 这样做的好处: 1,只要写好了一个功能模块,就可以在 ...

  5. python开发_python中的函数定义

    下面是我做的几个用列: #python中的函数定义,使用和传参 def_str = '''\ python中的函数以如下形式声明: def 函数名称([参数1,参数2,参数3......]): 执行语 ...

  6. python开发_python中的变量:全局变量和局部变量

    如果你在为python中的变量:全局变量和局部变量头疼,我想这篇blog会给你帮助 运行效果: 代码部分: #Python中的变量:全局变量和局部变量 #在很多语言中,在声明全局变量的时候,都喜欢把全 ...

  7. python开发_python中for循环操作

    如果你对python中的for循环不是很清楚,请看看这篇文章:”for循环控制语句——菜鸟的Python笔记“ 下面是我做的一些学习记录供大家参考: #基本的for循环语句 test_list = [ ...

  8. python开发_python中的list操作

    对python中list的操作,大家可以参考: Python list 操作 以下是我个人的笔记: ============================================ Add b ...

  9. python开发_python关键字

    python3.3.2中的关键字如下: The following identifiers are used as reserved words, or keywords of the languag ...

随机推荐

  1. 【日期控件】JQueryUI的datepicker日期控件

    在输入日期的时候我们经常需要日期控件,jQueryUI的datapicker就是一个很好的日期控件. 1.简单的datepicker控件 目录结构:(要将images图片放到css目录下面)

  2. linux自动创建dev node

    通过驱动模块的加载在/dev下创建设备文件,在驱动模块卸载时又自动的删除在/dev下创建的设备文件非常方便.而这个过程就是通过device_create()和device_destroy()内核函数完 ...

  3. sar命令使用【转】

    sar(System Activity Reporter系统活动情况报告)是目前 Linux 上最为全面的系统性能分析工具之一,可以从多方面对系统的活动进行报告,包括:文件的读写情况.系统调用的使用情 ...

  4. sql server 2008 r2 产品密钥

    数据中心版:PTTFM-X467G-P7RH2-3Q6CG-4DMYBDDT3B-8W62X-P9JD6-8MX7M-HWK38==================================== ...

  5. 136.Single Number---异或、位运算

    题目链接 题目大意:给出一串数组,里面的数都是两个,只有一个数是一个,把这个只有一个的数找出来.时间复杂度最好是线性的,空间复杂度最好为O(1). 法一:利用map,空间换时间,代码如下(耗时26ms ...

  6. 推荐一个数据相关的网站tushare

    推荐一个网站:tushare 使用方法如下: pip install tushare 我是使用pycharm直接安装的 抓取了浦发和光大的股票数据,并通过csv进行保存,和通过plt进行图片打印 im ...

  7. 非常粗糙的react网页ppt

    import React, {Component} from 'react'; import './slide.css'; class Page extends Component { constru ...

  8. 秀尔算法:破解RSA加密的“不灭神话” --zz

    http://netsecurity.51cto.com/art/201508/488766.htm RSA加密曾被视为最可靠的加密算法,直到秀尔算法出现,打破了RSA的不灭神话. RSA加密 VS ...

  9. TeX Live & TeXstudio 安装手记

    数据库课上又看到了那位用 beamer 做 slides 的师兄,想到自己一拖再拖的LaTeX入门,决定赶快动手装个环境再说~在经过一番搜索和研究之后决定先在 windows 底下试用,选择 TeX ...

  10. 关于HTML&CSS的笔记

    最近在看Jon Duckett的HTML&CSS一书(http://book.douban.com/subject/6585090/),书的排版和讲解方式很不错,并且有许多其他教学材料遗漏的关 ...