习题 28: 布尔表达式练习

上一节你学到的逻辑组合的正式名称是“布尔逻辑表达式(boolean logic expression)”。在编程中,布尔逻辑可以说是无处不在。它们是计算机运算的基础和重要组成部分,掌握它们就跟学音乐掌握音阶一样重要。

在这节练习中,你将在 python 里使用到上节学到的逻辑表达式。先为下面的每一个逻辑问题写出你认为的答案,每一题的答案要么为 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. 找到相等判断的部分 (== or !=),将其改写为其最终值 (True 或 False)。
  2. 找到括号里的 and/or,先算出它们的值。
  3. 找到每一个 not,算出他们反过来的值。
  4. 找到剩下的 and/or,解出它们的值。
  5. 等你都做完后,剩下的结果应该就是 True 或者 False 了。

下面我们以 #20 逻辑表达式演示一下:

3 != 4 and not ("testing" != "test" or "Python" == "Python")

接下来你将看到这个复杂表达式是如何逐级解为一个单独结果的:

  1. 解出每一个等值判断:
    1. 3 != 4 为 TrueTrue and not ("testing" != "test" or "Python" == "Python")
    2. "testing" != "test" 为 TrueTrue and not (True or "Python" == "Python")
    3. "Python" == "Python"True and not (True or True)
  2. 找到括号中的每一个 and/or :
    1. (True or True) 为 True: True and not (True)
  3. 找到每一个 not 并将其逆转:
    1. not (True) 为 False: True and False
  4. 找到剩下的 and/or,解出它们的值:
    1. True and False 为 False

这样我们就解出了它最终的值为 False.

Warning

复杂的逻辑表达式一开始看上去可能会让你觉得很难。而且你也许已经碰壁过了,不过别灰心,这些“逻辑体操”式的训练只是让你逐渐习惯起来,这样后面你可以轻易应对编程里边更酷的一些东西。只要你坚持下去,不放过自己做错的地方就行了。如果你暂时不太能理解也没关系,弄懂的时候总会到来的。

你应该看到的结果

以下内容是在你自己猜测结果以后,通过和 python 对话得到的结果:

$ python
Python 2.5.1 (r251:54863, Feb 6 2009, 19:02:12)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> True and True
True
>>> 1 == 1 and 2 == 2
True

加分习题

  1. Python 里还有很多和 != 、 == 类似的操作符. 试着尽可能多地列出 Python 中的等价运算符。例如 < 或者 <= 就是。
  2. 写出每一个等价运算符的名称。例如 != 叫 “not equal(不等于)”。
  3. 在 python 中测试新的布尔操作。在敲回车前你需要喊出它的结果。不要思考,凭自己的第一感就可以了。把表达式和结果用笔写下来再敲回车,最后看自己做对多少,做错多少。
  4. 把习题 3 那张纸丢掉,以后你不再需要查询它了。

最新文章

  1. PHPCMS导航栏当前栏目选中方法
  2. git tag使用标记
  3. LightOJ1283 Shelving Books(DP)
  4. hdu3652 B-number
  5. Scrum会议3(Beta版本)
  6. Entity Framework (二) 查询
  7. 今天,安装了一个GANGLIA玩玩,以后再测试NAGIOS吧。
  8. 数据库系统原理及其应用总结---ShinePans
  9. Ext JS学习第五天 我们所熟悉的javascript(四)
  10. hibernate异常:org.hibernate.MappingException
  11. 关于Jsp页面在ww:iterator 标签里面判断的写法是可以直接写数组里面的变量的
  12. 【Beta阶段】第七次scrum meeting
  13. java面试一、1.1基础
  14. [nodejs] nodejs开发个人博客(五)分配数据
  15. 【NLP】大白话讲解word2vec到底在做些什么
  16. 【NOI 2015】品酒大会
  17. GGTalk即时通讯系统(支持广域网)终于有移动端了!(技术原理、实现、源码)
  18. sqlalchemy操作----建表 插入 查询 删除
  19. QueryString to Dictionary&lt;string, string&gt;
  20. sys模块python

热门文章

  1. JAVA第一个程序hello world
  2. js删除数组中的 &quot;NaN&quot;
  3. day30 锁 队列
  4. daterangepicker-双日历
  5. 修改jupyter notebook的默认浏览器
  6. Redis未授权访问攻击过程与防范
  7. gitlab 邮件配置
  8. pod基本操作
  9. Head First 设计模式笔记(适配器)
  10. 2019.03.21 读书笔记 readonly与const