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. VueJS $refs 在 ElementUI 中遇到的问题

    表单验证的时候  $refs 拿不到 暂且是用 $nextTick 解决,具体原因有待研究 假入在 created 中注册时间来验证 validate,那就放在mounted中 或者...注册了 ev ...

  2. 老版本ubuntu更新源地址以及sources.list的配置方法 转

    转自(http://blog.csdn.net/snaking616/article/details/52966634) 1.国内可用的更新源地址: (1)中科大地址 http://mirrors.u ...

  3. html---规范、细节积累-01

    语义错误 块级元素可以包含内联元素和某些块级元素,内联元素不能包含块级元素,只能包含内联元素 页面可能正常解析,但不符合语义.浏览器自带容错机制,对于不规范的写法也能够正确解析,各浏览器的容错机制不同 ...

  4. 非交互式shell脚本案例-实现自主从oracle数据库获取相关数据,并在制定目录生成相应规则的文件脚本

    get_task_id 脚本内容 #!/usr/bin/expect#配置登陆数据库的端口set port 22#配置登陆数据库的ip地址set oracleip 10.0.4.41#配置数据库实例名 ...

  5. [转载]C++多态技术

    摘自: http://www.royaloo.com/articles/articles_2003/PolymorphismInCpp.htm http://blog.sciencenet.cn/bl ...

  6. rpmdb: Thread/process 9180/139855524558592 failed: Thread died in Berkeley DB library

    使用yum安装出现问题:rpmdb: Thread/process 9180/139855524558592 failed: Thread died in Berkeley DB library 解决 ...

  7. 数据分析python应用到的ggplot

    数据分析中应用到python中的ggplot库,可以用来画图 数据之类的用优达学院中课程七中的数据为例 数据是:https://s3.amazonaws.com/content.udacity-dat ...

  8. 【ios开发之疑难杂症】xcode运行出现SpringBoard 无法启动应用程序(错误:7)

    问题:xcode运行出现SpringBoard 无法启动应用程序(错误:7) 解决方案: 重启模拟器

  9. MVC – 9.mvc整体请求流程

    1.请求管道 2~5微软自己的验证,我们一般不用. 在全局配置文件中-已经配置一个路由过滤器-为第7个事件注册了路由方法 1.在application_start中向静态路由表注册了路由数据,在管道第 ...

  10. 关于Android不同系统版本的市场占比情况详解

    一,google官方统计的不同Android版本市场的占比强开 google统计的数据情况 这个是google官方对于不同版本的市场占比情况.这个是针对全世界所有的Android手机占比情况. 二,友 ...