Python学习笔记:03 布尔值和逻辑判断


本篇讲述Python的基本条件语句和布尔运算。可以在如下位置下载本篇涉及的所有代码:
https://code.csdn.net/snippets/188637/master/IfElseAndBoolean.py/raw

基本if、else语句

# Standard if / else statement
print 'Standard if / else statement test:'
i = 2
print 'The value if i is:', i
if i == 1:
    print 'Entered branch 1.'
elif i == 2:
    print 'Entered branch 2.'
elif i == 3:
    print 'Entered branch 3.'
else:
    print 'Entered branch other'


注:
* Python 中以缩进而不是花括号来标记代码块。If语句、函数均是如此。缩进应当是4个空格,不能是tab。
* 代码块需要以一个冒号开始。If语句、函数均如此
* Python 中没有switch语句,若要实现switch功能,需用多个“elif”表示。

简化的单行版 if、else

# Simplified format.
print ''
print 'One line if / else format test:'
i = 2
print 'true' if i == 2 else 'false'

注:整个表达式返回一个值,若测试条件为真则返回if之前的值,否则返回else之后的值。

直接赋布尔值

# Direct boolean value assignment
print ''
print 'Direct boolean value assignment test:'
myF = False
myT = True
print 'myF =', myF, ', myT = ', myT

注:
* “True”和“False”是大小写敏感的。首字母小写将被认为是普通变量。

整数作为布尔值

与C等语言类似,Python支持整数作为布尔值。0表示False,任何非0表示True。记忆方法很简单:二进制表示时,0表示低电平(无电压),1表示高电平(有电压)。无论通过多少个bit来表示整数,只有0这种情况是所有bit上全0,也就是“无”,自然是“False”。其它情况都是有电压的,也就是“有”,自然是“True”的。
# Integer as boolean
print ''
print 'Integer as boolean test'
i = 1
if i:
    print 'Integer value', i, 'means True'
else:
    print 'Integer value', i, 'means False'
print 'bool(2)', bool(2)
print 'bool(1)', bool(1)
print 'bool(0)', bool(0)
print 'bool(-1)', bool(-1)
print 'bool(-2)', bool(-2)

注:
* 在if等需要做逻辑判断的地方,可以直接使用整型来判断,系统会自动转换为布尔型。
* 内置的bool类可以显式地将整型转为布尔型。

比较运算符

# Comparison operators
print ''
print 'Comparison operators test:'
i = 2
print 'The value if i is:', i
print '----------------------------'
print 'i < 1:', i < 1
print 'i > 1:', i > 1
print 'i == 1:', i == 1
print 'i != 1:', i != 1
print 'i <= 2:', i <= 2
print 'i >= 3:', i >= 3
print '############################'


代码已经足够说明问题,不多解释

String比较

# Comparison of String
print ''
print 'String comparison test:'
strA = 'The String'
strB = 'The String'
strC = 'The other String'
print "a, b, c:", strA, strB, strC
print '------------------------------'
print 'if a == b:', strA == strB
print 'if a >= b:', strA >= strB
print 'if a == c:', strA == strC
print 'if a < c:', strA < strC
print '##############################'


注:
* 与Java要求使用“euqals()”函数来比较不同,Python的“==”操作符是比较值而不是引用,因此可以直接用比较运算符来比较两个字符串。字符串不仅支持相等比较,还支持大小(使用>、<)比较。
* 大小比较与目录排序顺序相同。


布尔运算符

# And, or, not
print ''
print 'Test of and, or, not:'
i = 2
print 'The value of i is:', i
print '------------------------------------------------'
print "i < 3 and myF:", i < 3 and myF
print "i < 3 or myF:", i < 3 or myF
print "not(i < 3 or myF):", not(i < 3 or myF)
print "(i < 3 or myF) and myF:", (i < 3 or myF) and myF
print '################################################'


全“或”,以及全“与非”逻辑的快捷实现

如果想要测试这样逻辑:“if (i == 1 or i == 2 or i == 3 ...)”,一种简单的方式是使用这样的写法:“if i in (1, 2, 3, ...)”
类似地,如果要测试这样的逻辑“if (i != 1 and i != 2 ...)”,可以使用这种写法:“if i not in (1, 2, 3, ...)”
代码如下:
# All 'or' and all 'and not'.
print ''
i = 4
print 'Now i is:', i
print 'will test whether i is in (1, 2, 3, 4, 5)'
if i in (1, 2, 3, 4, 5):
    print 'true'
else:
    print 'false'
print 'will test whether i is not in (1, 2, 3, 4, 5)'
if i not in (1, 2, 3, 4, 5):
    print 'true'
else:
    print 'false'

事实上,关键字“in”是用于遍历一个iterable的对象,例如touple(元组)、list(列表)或dictionary(字典),将在以后详细介绍。上述代码实际上创建了一个touple,并用in来遍历,只要发现匹配,就进入逻辑。
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值