更新时间:2023-11-29 15:43:58
布尔类型已添加到 Python 2.3.添加了两个新常量到 builtin 模块,True 和 False.(真假常数已添加到 Python 2.2.1 的内置函数中,但 2.2.1 版本被简单地设置为 1 和 0 的整数值并且没有什么不同输入.)
A Boolean type was added to Python 2.3. Two new constants were added to the builtin module, True and False. (True and False constants were added to the built-ins in Python 2.2.1, but the 2.2.1 versions are simply set to integer values of 1 and 0 and aren't a different type.)
bool
是 int
的子类,有两个单例值,True
和 False
,它们等于1
和 0
分别.Python 在内部进行这种转换.请参阅以下示例:
bool
is a subclass of int
with two singleton values, True
and False
, that are equal to 1
and 0
respectively. Python does this conversion internally.
See the below samples:
print(True + 1) # outputs 2
print(True - 1) # outputs 0
print(True - 2) # outputs -1
a = True
print(a) # outputs True because no mathematical calculation
print(True + True + 1) # Outputs 3
print(False + 1) # Outputs 3
print(False + True) # Outputs 1
参考: