题目
上题我们学习了不少逻辑表达式,但是他们还有另一个更正式的名字——布尔逻辑表达式(boolean logic expression)。它们无处不在非常重要,所以本题将要练习它们。
我们要做的是判断下列表达式的结果是 True 还是 False,并在 python 环境中验证结果:
- True and True
- False and True
- 1 == 1 and 2 == 1
- “test” == “test”
- 1 == 1 or 2 != 1
- True and 1 == 1
- False and 0 != 0
- True or 1 == 1
- “test” == “testing”
- 1 != 0 and 2 == 1
- “test” != “testing”
- “test” == 1
- not (True and False)
- not (1 == 1 and 0 != 1)
- not (10 == 1 or 1000 == 1000)
- not (1 != 10 or 3 == 4)
- not (“testing” == “testing” and “Zed” == “Cool Guy”)
- 1 == 1 and not (“testing” == 1 or 1 == 0)
- “chunky” == “bacon” and not (3 == 4 or 3 == 3)
- 3 == 3 and not (“testing” == “testing” or “Python” == “Fun”)
在最后将附上解题技巧。
加分练习
- python 中还有很多和
!=、==类似的操作符,试着尽可能的列出 python 的等价运算符,例如<或<=(我想这不是叫比较运算符么?)。 - 写出每一个等价运算符的名称
- 在 python 中测试新的布尔操作。在敲回车前喊出它们的结果。不要思考凭第一感觉就行,把表达式写在纸上在按下回车,最后看看自己做对多少,做错多少。
- 把习题 3 那张纸丢掉,以后用不到它了。
我的答案
28.0基础练习
# 1. True and True
print('True and True' + ' 是真是假? 我的答案是 ' + 'True')
print(True and True, "\n" * 2)
# 2. False and True
print('False and True' + ' 是真是假? 我的答案是 ' + 'False')
print(False and True, "\n" * 2)
# 3. 1 == 1 and 2 == 1
print('1 == 1 and 2 == 1' + ' 是真是假? 我的答案是 ' + 'F

本文通过《笨办法学Python3》中的练习,介绍了布尔逻辑表达式的重要性和应用,包括True和False的判断,比较运算符的使用如等于、不等于,并通过实际例子验证结果。此外,还提到了加分练习,要求列出Python中的等价运算符及其名称,并进行测试。
最低0.47元/天 解锁文章
2万+

被折叠的 条评论
为什么被折叠?



