复习Python的Day 26
发布日期:2021-07-01 03:03:06 浏览次数:2 分类:技术文章

本文共 1045 字,大约阅读时间需要 3 分钟。

复习Python的第二十六天

习题28: 布尔表达式练习

“布尔逻辑表达式(boolean logic expression)”在程序中很常见,它们经常用于条件测试,而且用布尔表达式还能编写出一些精巧的语句。因此我们必须掌握。

>>> True and True True>>>  False and TrueFalse>>>  1 == 1 and 2 == 1False>>>  "test" == "test" True>>>  1 == 1 or 2 != 1 True>>>  True and 1 == 1True>>>  False and 0 != 0 False>>>  True or 1 == 1 True>>>  "test" == "testing" False>>>  1 != 0 and 2 == 1 False>>>  "test" != "testing" True>>>  "test" == 1 False>>>  not (True and False)True>>>  not (1 == 1 and 0 != 1)False >>>  not (10 == 1 or 1000 == 1000) False>>>  not (1 != 10 or 3 == 4) False>>>  not ("testing" == "testing" and "Zed" == "Cool Guy") True>>>  1 == 1 and not ("testing" == 1 or 1 == 0) True>>>  "chunky" == "bacon" and not (3 == 4 or 3 == 3) False>>>  3 == 3 and not ("testing" == "testing" or "Python" == "Fun" )False

判断步骤:

  1. 找到相等判断的部分 (== or !=),将其改写为其最终值 (True 或 False)。
  2. 找到括号里的 and/or,先算出它们的值。
  3. 找到每一个not,算出它们反过来的值。
  4. 找到剩下的 and/or,解出它们的值。
  5. 等你都做完后,剩下的结果应该就是 True 或者 False 了。

这个步骤很合理,因为相不相等一眼就看得出来,而寻找括号是因为其优先级最高,not的判断也很简单。

这样清理下来,剩下的就简单了,我们也不需要从头看到尾、从左至右地扫视。那样吃力不讨好。

转载地址:https://memcpy0.blog.csdn.net/article/details/79886803 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:复习Python的Day 27
下一篇:复习PythonDay25

发表评论

最新留言

第一次来,支持一个
[***.219.124.196]2024年04月27日 18时40分38秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章