练习28--布尔练习

一 解题步骤

不论何时,当你看到这些布尔逻辑表达式,你可以通过以下简单的几步来解决它们:
1. 把每一个相等性测试(== 或者 !=)替换成真实性测试。
2. 先解决圆括号里面的 and/or。
3. 找到每一个 not,然后把它反转过来。
4. 找到剩余的 and/or,然后解决掉。
5. 当你完成的时候,你应该得到 True 或者 False。
二 注意事项
1 为什么 "test" and "test" 返回的是 test,1 and 1 返回的是 1 而不是 True? Python 和其他很多语言一样,喜欢返回布尔表达式的运算数而不是只是 True 或者 False。这意味着,如果是 False and1,你会得到第一个运算数(False),如果是 True and 1,你会得到第二个运算数(1),试着玩玩这个。
2 有捷径吗??有,任何包含一个 False 的 and 表达式结果都是 False。任何包含一个 True 的 or表达式结果都是 True。但是你要掌握处理整个表达式的过程,后面会用到。
三 一些测试
 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"))
原文地址:https://www.cnblogs.com/luoxun/p/13283251.html