复习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
判断步骤:
- 找到相等判断的部分 (== or !=),将其改写为其最终值 (True 或 False)。
- 找到括号里的 and/or,先算出它们的值。
- 找到每一个not,算出它们反过来的值。
- 找到剩下的 and/or,解出它们的值。
- 等你都做完后,剩下的结果应该就是 True 或者 False 了。
这个步骤很合理,因为相不相等一眼就看得出来,而寻找括号是因为其优先级最高,not的判断也很简单。
这样清理下来,剩下的就简单了,我们也不需要从头看到尾、从左至右地扫视。那样吃力不讨好。
转载地址:https://memcpy0.blog.csdn.net/article/details/79886803 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
第一次来,支持一个
[***.219.124.196]2024年04月27日 18时40分38秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Search Insert Position
2019-05-02
Length of Last Word
2019-05-02
QuickSort快速排序
2019-05-02
hdu2052
2019-05-02
ubuntu小技巧
2019-05-02
c语言细读之static auto
2019-05-02
ffmpeg开发中的问题(九)
2019-05-02
leetcode-longest common prefix
2019-05-02
tinyhttpd源码学习1
2019-05-02
九度题目1015:还是A+B
2019-05-02
Mongoose API Reference
2019-05-02
hdu2568
2019-05-02
九度题目1505:两个链表的第一个公共结点
2019-05-02
linux下intel 82579LM 网卡驱动安装
2019-05-02
ffmpeg开发中的问题(十二) 一些小点
2019-05-02
python urllib
2019-05-02
APUE初学 环境搭建
2019-05-02
Binary Tree Level Order Traversal
2019-05-02
题目1511:从尾到头打印链表
2019-05-02
SYSTEM V IPC 基本概念
2019-05-02