《笨办法学 python3》系列练习计划——28.布尔表达式练习

题目

上题我们学习了不少逻辑表达式,但是他们还有另一个更正式的名字——布尔逻辑表达式(boolean logic expression)。它们无处不在非常重要,所以本题将要练习它们。
我们要做的是判断下列表达式的结果是 True 还是 False,并在 python 环境中验证结果:

  1. True and True
  2. False and True
  3. 1 == 1 and 2 == 1
  4. “test” == “test”
  5. 1 == 1 or 2 != 1
  6. True and 1 == 1
  7. False and 0 != 0
  8. True or 1 == 1
  9. “test” == “testing”
  10. 1 != 0 and 2 == 1
  11. “test” != “testing”
  12. “test” == 1
  13. not (True and False)
  14. not (1 == 1 and 0 != 1)
  15. not (10 == 1 or 1000 == 1000)
  16. not (1 != 10 or 3 == 4)
  17. not (“testing” == “testing” and “Zed” == “Cool Guy”)
  18. 1 == 1 and not (“testing” == 1 or 1 == 0)
  19. “chunky” == “bacon” and not (3 == 4 or 3 == 3)
  20. 3 == 3 and not (“testing” == “testing” or “Python” == “Fun”)

在最后将附上解题技巧。

加分练习

  1. python 中还有很多和 !=== 类似的操作符,试着尽可能的列出 python 的等价运算符,例如 <<= (我想这不是叫比较运算符么?)。
  2. 写出每一个等价运算符的名称
  3. 在 python 中测试新的布尔操作。在敲回车前喊出它们的结果。不要思考凭第一感觉就行,把表达式写在纸上在按下回车,最后看看自己做对多少,做错多少。
  4. 把习题 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' + ' 是真是假? 我的答案是 ' + 'False')
print(1 == 1 and 2 == 1, "\n" * 2)

# 4. "test" == "test"
print('"test" == "test"' + ' 是真是假? 我的答案是 ' + 'True')
print("test" == "test", "\n" * 2)

# 5. 1 == 1 or 2 != 1
print('1 == 1 or 2 != 1' + ' 是真是假? 我的答案是 ' + 'True')
print(1 == 1 or 2 != 1, "\n" * 2)

# 6. True and 1 == 1
print('True and 1 == 1' + ' 是真是假? 我的答案是 ' + 'True')
print(True and 1 == 1, "\n" * 2)

# 7. False and 0 != 0
print('False and 0 != 0' + ' 是真是假? 我的答案是 ' + 'False')
print(False and 0 != 0, "\n" * 2)

# 8. True or 1 == 1
print('True or 1 == 1' + ' 是真是假? 我的答案是 ' + 'True')
print(True or 1 == 1, "\n" * 2)

# 9. "test" == "testing"
print('"test" == "testing"' + ' 是真是假? 我的答案是 ' + 'False')
print("test" == "testing", "\n" * 2)

# 10. 1 != 0 and 2 == 1
print('1 != 0 and 2 == 1' + ' 是真是假? 我的答案是 ' + 'False')
print(1 != 0 and 2 == 1, "\n" * 2)

# 11. "test" != "testing"
print('"test" != "testing"' + ' 是真是假? 我的答案是 ' + 'True')
print("test" != "testing", "\n" * 2)

# 12. "test" == 1
print('"test" == 1' + ' 是真是假? 我的答案是 ' + 'False')
print("test" == 1, "\n" * 2)

# 13. not (True and False)
print('not (True and False)' + ' 是真是假? 我的答案是 ' + 'True')
print(not (True and False), "\n" * 2)

# 14. not (1 == 1 and 0 != 1)
print('not (1 == 1 and 0 != 1)' + ' 是真是假? 我的答案是 ' + 'False')
print(not (1 == 1 and 0 != 1), "\n" * 2)

# 15. not (10 == 1 or 1000 == 1000)
print('not (10 == 1 or 1000 == 1000)' + ' 是真是假? 我的答案是 ' + 'False')
print(not (10 == 1 or 1000 == 1000), "\n" * 2)

# 16. not (1 != 10 or 3 == 4)
print('not (1 != 10 or 3 == 4)' + ' 是真是假? 我的答案是 ' + 'False')
print(not (1 != 10 or 3 == 4), "\n" * 2)

# 17. not ("testing" == "testing" and "Zed" == "Cool Guy")
print('not ("testing" == "testing" and "Zed" == "Cool Guy")' +
 ' 是真是假? 我的答案是 ' + 'True')
print(not ("testing" == "testing" and "Zed" == "Cool Guy"), "\n" * 2)

# 18. 1 == 1 and not ("testing" == 1 or 1 == 0)
print('1 == 1 and not ("testing" == 1 or 1 == 0)' +
 ' 是真是假? 我的答案是 ' + 'True')
print(1 == 1 and not ("testing" == 1 or 1 == 0), "\n" * 2)

# 19. "chunky" == "bacon" and not (3 == 4 or 3 == 3)
print('"chunky" == "bacon" and not (3 == 4 or 3 == 3)' +
 ' 是真是假? 我的答案是 ' + 'False')
print("chunky" == "bacon" and not (3 == 4 or 3 == 3), "\n" * 2)

# 20. 3 == 3 and not ("testing" == "testing" or "Python" == "Fun")
print('3 == 3 and not ("testing" == "testing" or "Python" == "Fun")' +
 ' 是真是假? 我的答案是 ' + 'False')
print(3 == 3 and not ("testing" == "testing" or "Python" == "Fun"), "\n" * 2)

运行一下一次成功 100 分!
这里写图片描述

28.1 比较运算符 + 28.2 比较运算符的名称

我手里的这本《笨办法学python》中这里叫做 等价运算符 ,说实话我特意搜了一下这个词但是没有东西叫这个名,所以估计它说的应该是下面这些比较运算符了。

比较运算符 名字
== 等于
!= 不等于
< 小于
> 大于
<= 小于等于
>= 大于等于




返回目录
《笨办法学 python3》系列练习计划——目录

猜你喜欢

转载自blog.csdn.net/aaazz47/article/details/79698473