布尔bool表达式

注:这是自己跟着慕课嵩天老师的课程写的笔记。笔记若有错误,希望大家批评指正。

一、布尔操作符:and、or、not

and相与(乘法),or相或(加法),not取反
优先级:not、and、or
a or not b and c = (a or ((not b) and c))

二、性质

a or ture = ture
a and false = false

分配率:
a or (b and c) = (a or b) and (a or c)
a and (b or c) = (a and b) or (a and c)

not (not a) = a

德摩根定律
not(a or b) = (not a) and (not b)
not(a and b) = (not a) or (not b)
‘’’

三、特点

1.数字为零值、空字符串、空列表都被认为是false;数字为非零值、非空字符串、非空列表认为是true;bool类型仅仅是一个特殊的整数,true = 1,false = 0

print("----特点1----")
print(True+True)
print(False+True)
print(False+False)

print(bool(0))
print(bool(1))
print(bool(32))
print(bool("hello"))
print(bool(""))
print(bool("\n"))
print(bool([1,2,3]))
print(bool([]))

2.布尔运算从左到右扫描表达式一旦直到结果,就立即返回Ture或者False
内部操作逻辑如下:
x and y:If x is false, return x; otherwise, return y
x or y:If x is true, return x; otherwise, return y
not x :If x is false, return true; otherwise, return false

print("----特点2----")
response = input("yes or no")
while response[0] == 'Y' or response[0] == 'y':
    print("yes")
    response = input("yes or no")

while response[0] == 'Y' or 'y':
    print("dead")

注:
while response[0] == ‘Y’ or response[0] == ‘y’: 不是死循环
while response[0] == ‘Y’ or ‘y’: 是死循环
因为布尔运算时or右侧的’y’始终是真,所以无论首字母是否为’Y’,该表达式均为真

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值