明确的一点是boolean值不是python专有的,其他编程措辞java javascript php等实在都有
布尔值是 真 True 或 假 False 中的一个。.脚本也会在适当时将值 True 和 False 转换为 1 和 0。.布尔值(bool),是python内置数据类型的一种,属于整型(int)的子类,True和False都是有对应的数值,True=1,False=0。可以做数值运算的,也便是加减乘除等。
实在我们可以看到bool类型的值的事理定义 class bool(int):

这个表里bool值是int 整数的一个子类,后面学习面向工具的时候会详细先容
class bool(int): """ bool(x) -> bool Returns True when the argument x is true, False otherwise. The builtins True and False are the only two instances of the class bool. The class bool is a subclass of the class int, and cannot be subclassed. """
>>> a = True>>> b = False>>> type(a)<class 'bool'>>>> type(b)<class 'bool'>>>>
>>> a = True>>> b = False>>> c = a+b>>> type(c)<class 'int'>>>> c1>>>>>> True ==1True>>> False == 0True>>>
看一下实际运算效果
布尔运算
既然我们知道True,False是两个分外的整数,实在是bool类型匠变量,那么它们的浸染肯定 不是用在普通 的加法减法乘法除尘上了,它有自己的运算
布尔运算,包含三个布尔操作符,即 and ,。or , not
优先级:not > and > or
怎么理解 这三个运算以及它们的会选 级的观点。
先有布尔 值怎么产生布尔值怎么运算布尔值怎么产生呢?我们可以理解为数学比较运算得出的一个结果
大于 即是 小于 不即是 存在,不存在
怎么理解?看几个例子
办理了bool值来源的问题,接着看怎么用
>>> print(10 > 9)True>>> print(10 == 9)False>>> print(10 < 9)False>>>
即 and ,。or , not 运算符号
print(False or True and False) #Falseprint(True or not True and False) #Trueprint(not False and True or not True) #True
bool值实质 是真假,那么 该当怎么用呢
条件语句中利用bool值,后面在讲掌握语句会详细讲解,这里有个直不雅观的认识即可。
a = 1b = 211if b > a: print("b 大于 a")else: print("b小于即是 a")
if 真: 实行代码else: 实行代码
如果a>b成立 则实行代码,否则实行另一个逻辑,
实在if的实质 是后面的bool值是true就知足了。
其他类型的数据能变成bool值 True
x = "Hello"y = 121print(bool(x))print(bool(y))
如何天生bool值 False
>>> bool(0)False>>> bool(-1)True>>> bool('')False>>> bool(None)False>>> >>> bool(null)Traceback (most recent call last):File "<pyshell#34>", line 1, in <module>bool(null)NameError: name 'null' is not defined
bool值代表的是真或假,常日会与如果if while for等掌握语句结合用用来表里知足某种条件时,实行对应 的程序逻辑