Python--Demo5--布尔类型相关操作

布尔类型:

我们已经知道布尔类型只有两个值True和False

布尔类型的操作:

比较运算、逻辑运算;操作结果都是布尔值。

比较运算符:

完成比较运算需要知道,比较运算符。(== 等于)(!= 不等于)(> 大于) (<小于)(<= 小于等于)(>= 大于等于)

示例:

>>> 33.0==33
True
>>> 'hehe'=='hehe'
True
>>> 'HeHe'=='hehe'
False
>>> 3!=3
False
>>> 3==3
True
>>> 3>2
True
>>> 3=='3'
False
>>> 'a'>1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: '>' not supported between instances of 'str' and 'int'

说明:>、<、>=、<= 只能用于整型和浮点型之间

布尔操作符:

布尔操作符是两个布尔值之间的运算,布尔操作符有 and  or  not

示例:

>>> True and False
False
>>> True or False
True
>>> not True
False
>>> (1==1) and (1>=2)
False
>>> (1==1) or (1>=2)
True
>>> 2+2 == 4 and not 2+2==5 and 2*2 ==2+2
True
>>> "bobo" and False
False

说明:布尔操作符优先级 not最高、其次时and 和or

猜你喜欢

转载自www.cnblogs.com/bigbosscyb/p/12318351.html
今日推荐