python逻辑运算符的使用_总结Python中逻辑运算符的使用

下表列出了所有python语言支持的逻辑运算符。假设变量a持有10和变量b持有20,则:

9777a7f06e44056570209e6ee0152557.png

示例:

试试下面的例子就明白了所有的python编程语言提供了逻辑运算符:

#!/usr/bin/python

a = 10

b = 20

c = 0

if ( a and b ):

print "line 1 - a and b are true"

else:

print "line 1 - either a is not true or b is not true"

if ( a or b ):

print "line 2 - either a is true or b is true or both are true"

else:

print "line 2 - neither a is true nor b is true"

a = 0

if ( a and b ):

print "line 3 - a and b are true"

else:

print "line 3 - either a is not true or b is not true"

if ( a or b ):

print "line 4 - either a is true or b is true or both are true"

else:

print "line 4 - neither a is true nor b is true"

if not( a and b ):

print "line 5 - either a is not true or b is not true"

else:

print "line 5 - a and b are true"

当执行上面的程序它会产生以下结果:

line 1 - a and b are true

line 2 - either a is true or b is true or both are true

line 3 - either a is not true or b is not true

line 4 - either a is true or b is true or both are true

line 5 - either a is not true or b is not true

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值