前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python开发_python中的Boolean运算和真假值

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

作者头像
Hongten
发布2018-09-13 12:53:30
9670
发布2018-09-13 12:53:30
举报
文章被收录于专栏:Hongten

python中的真假值:

Truth Value Testing

代码语言:javascript
复制
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.)

总结:

代码语言:javascript
复制
#在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

代码语言:javascript
复制
 1 print('x or y -> if x is false,then y, else x ')
 2 x, y = 2, 0
 3 print('{} or {} = {}'.format(x, y, x or y))
 4 x1, y1 = 0, 10
 5 print('{} or {} = {}'.format(x1, y1, x1 or y1))
 6 x2, y2 = 0, 0
 7 print('{} or {} = {}'.format(x2, y2, x2 or y2))
 8 
 9 print('#' * 50)
10 print('x and y -> if x is false,then x, else y ')
11 print('{} and {} = {}'.format(x, y, x and y))
12 x1, y1 = 0, 10
13 print('{} and {} = {}'.format(x1, y1, x1 and y1))
14 x2, y2 = 0, 0
15 print('{} and {} = {}'.format(x2, y2, x2 and y2))
16 
17 print('#' * 50)
18 print('not x -> if x is false,then True,else False ')
19 x = 2
20 print('not {} = {}'.format(x, not x))
21 x = 0
22 print('not {} = {}'.format(x, not x))

运行效果:

代码语言:javascript
复制
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
>>> 
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2013-07-29 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档