Skip to content

Latest commit

 

History

History
189 lines (141 loc) · 3.24 KB

File metadata and controls

189 lines (141 loc) · 3.24 KB
Error in user YAML: (<unknown>): found a tab character that violate indentation while scanning a plain scalar at line 3 column 3
---
- oeasy Python 0155
- 这是 oeasy 系统化 Python 教程,从基础一步步讲,扎实、完整、不跳步。愿意花时间学,就能真正学会。
- 本教程同步发布在: 
	- 个人网站: `https://oeasy.org` 
	- 蓝桥云课: `https://www.lanqiao.cn/courses/3584` 
	- GitHub: `https://github.com/overmind1980/oeasy-python-tutorial` 
	- Gitee: `https://gitee.com/overmind1980/oeasypython` 
---

比较运算符

回忆

  • 上次 在条件 里面添加了 条件
    • 缩进结构
      • 既是样式
      • 也是逻辑关系

图片描述

  • 嵌套可以有很多很多层
    • 判断谁
    • 判断谁
    • 有什么说法吗?🤔

避免无效嵌套

  • 没必要 判断两次
    • 满足 x == 10
    • 一定满足 x < 50

图片描述

  • 再看 下面代码

修改代码

  • 这个代码
    • 如果让你修改
i = 15
s = "oeasy"
if i >= 15:
	if s == "oeasy":
		print("more than",s)
if i < 15:
	if s == "oeasy":
		print("less than",s)
  • 应该如何修改?

修改结构

  • 两个if之间是互斥的关系
    • 可以把第二个if省略
    • if + else
i = 15
s = "oeasy"
if i >= 15:
	if s == "oeasy":
		print("more than",s)
else:
	if s == "oeasy":
		print("less than",s)
  • 两个分支后面
    • 无论哪个分支 都得判断s
    • 可以 上来就判断s

判断次序

  • 先判断 s 之后
    • 后 判断 i
i = 15
s = "oeasy"
if s == "oeasy":	
	if i >= 15:
		print("more than",s)
	else:
		print("less than",s)
  • 两个条件
s == "oeasy":	
i >= 15
  • 先判断 哪个好??

根据概率来

  • 一般概率低的 放在外面
    • 能做 一次判断
    • 就不做 两次判断
  • 需要具体情况具体分析

图片描述

  • 这先后次序 有讲究

bmi

图片描述

  • 判断是否肥胖

图片描述

编写代码

  • 编写的代码是这样的
bmi = 25
if bmi >= 25:
    if bmi >= 30:
        print("Obese!")
    else:
        print("Overweight!")
else:
    if bmi >= 18.5:
        print("Normal!")
    else:
        print("Underweight!")
  • 逻辑结构如图

图片描述

  • 嵌套条件结构
    • 就是 if 嵌套
  • 目前两层

两仪生四象

  • if-else
    • 就是 具体情况
    • 具体分析

图片描述

  • 分析分析
    • 开了 辨
    • 这么着 还是 那么着
    • 阴阳之下 又分 阴阳

自然界

  • 因此可以分门别类

图片描述

总结

  • 这次思考了 条件的次序
    • 都有的 子判断
      • 可以 提到外面
    • 概率低的 判断
      • 可以 放在前面
  • 二分法
    • dichotomy [daɪˈkɒtəmi]
    • dicho-
      • 来自di-的希腊语形式
    • -tomy
      • 砍,切
      • 词源同 atom, anatomy
      • 即切成两半的

图片描述

  • 问题就是 嵌套层次太多了
    • 可以 搞扁平了吗?🤔
  • 下次再说 👋

  • 本文来自 oeasy Python 系统教程。
  • 想完整、扎实学 Python,
  • 搜索 oeasy 即可。