【再回首Python之美】【逻辑运算】七个逻辑运算符:not, or, and, not or, not and, !=, ==

七个逻辑运算符

 not

  ret = not condition,not的结果和condition的值相反

or,

  ret = left or right,只要有一个值为True,or的结果就是True

and

  ret = left and right,只要有一个值为False,and的结果就是False

not or

  ret = not(left or right), 只有left和right有一个为True时,not or的结果就是False

not and

  ret = not(left and right),只有left和right有一个为False时,not and的结果就是True

!=

  ret = left != right,left和right的值不相等,!=的结果就是True

==

  ret = left == right,left和right的值相等,==的结果就是True

示例代码

#ex_logicOperation.py
self_file = __file__ #save current file absolute path

#逻辑运算
#not, or, and
#not or, not and
#!=,==

#not
#not condition,not的结果和condition的值相反
print not False #True
print not True  #False

#or
#left or right,只要有一个值为True,or的结果就是True
print True or False     #True
print True or True      #True
print False or True     #True
print False or False    #False

#and
#left and right,只要有一个值为False,and的结果就是False
print True and False    #False
print True and True     #True
print False and True    #False
print False and False   #False

#not or
#not(left or right), 只有left和right有一个为True时,not or的结果就是False
print not(True or False)    #False
print not(True or True)     #False
print not(False or True)    #False
print not(False or False)   #True

#not and
#not(left and right),只有left和right有一个为False时,not and的结果就是True
print not(True and False)   #True
print not(True and True)    #False
print not(False and True)   #True
print not(False and False)  #True

#!=
#left != right,left和right的值不相等,!=的结果就是True
print 1 != 0    #True
print 1 != 1    #False
print 0 != 0    #False
print 0 != 1    #True

#==
#left == right,left和right的值相等,==的结果就是True
print 1 == 0    #False
print 1 == 1    #True
print 0 == 0    #True
print 0 == 1    #False

print "\nexit %s" % self_file

编译执行


(end)

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值