Python中的布尔类型的注意点
来源:互联网 发布:linux自动挂载硬盘 编辑:程序博客网 时间:2024/05/16 12:56
一.前戏:
3. Data model
3.2. The standard type hierarchy¶
- Booleans (
bool
) These represent the truth values False and True. The two objects representing the values
False
andTrue
are the only Boolean objects. The Boolean type is asubtypeof the integer type, and Boolean valuesbehavelike the values 0 and 1, respectively, in almost all contexts, the exception being that when converted to a string, the strings"False"
or"True"
are returned, respectively.
The rules for integer representation are intended to give the most meaningful interpretation of shift and mask operations involving negative integers.
使用整数表示的规则主要是为了在涉及到负数的移位和掩码操作更有意义
4. Built-in Types
4.1. 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 consideredfalse:
None
False
zero of any numeric type, for example,
0
,0.0
,0j
.any empty sequence, for example,
''
,()
,[]
.any empty mapping, for example,
{}
.instances of user-defined classes, if the class defines a
__bool__()
or__len__()
method, when that method returns the integer zero orbool
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.)
def f():passclass Foo():passif 3:print(3)if '3':print('3')if (3,):print((3,))if[3]:print([3])if {3:'3'}:print({3:'3'})if f:print(f)if Foo:print(Foo)'''输出:33(3,)[3]{3: '3'}<function f at 0x0000018CDD713E18><class '__main__.Foo'>'''
if 0:print('0--if')else:print('0--else')if '':print('string--if')else:print('string--else')if ():print('tuple--if')else:print('tuple--else')if []:print('list--if')else:print('list--else')if {}:print('dic--if')else:print('dic--else')'''输出:0--elsestring--elsetuple--elselist--else{}--else'''
if 0:print('0--if')else:print('0--else')if '':print('string--if')else:print('string--else')if ():print('tuple--if')else:print('tuple--else')if []:print('list--if')else:print('list--else')if {}:print('dic--if')else:print('dic--else')'''输出:0--elsestring--elsetuple--elselist--else{}--else'''
- Python中的布尔类型的注意点
- Python中的布尔类型
- Python中的布尔类型
- API、修饰符、布尔类型注意点
- Python中进行布尔类型处理时的特殊点
- 每天学点Python之布尔类型
- 1.python类型注意点
- Objective-C中的布尔类型的用法
- Objective-C中的布尔类型的用法
- 布尔类型判断-python
- python 布尔类型
- Python中布尔类型
- Python中布尔类型
- Python中布尔类型
- Python中布尔类型
- Python (布尔类型计算)
- Delphi中的布尔类型
- C中的布尔类型
- 可数集
- PAT1062 最简分数
- 如何激活xp
- BZOJ4881 [Lydsy2017年5月月赛]线段游戏
- [bzoj] 1242: Zju1015 Fishing Net弦图判定
- Python中的布尔类型的注意点
- linux中线程ID与进程ID
- Egyptian Fractions (HARD version) UVA
- 信息系统分析与设计----系统分析概述
- jvm什么时候会触发full gc
- android socket java和C通信
- SylixOS块设备CACHE管理
- JSCPC 2017 参赛总结
- 2004年分区联赛提高组之三 合唱队形(dp)