简述

3. 基本数据类型 - 图1

介绍 Python 中的两种基本数据类型:

  • 数字类型(整数、浮点数和复数)
  • 字符串类型

介绍 time 库的基本使用,掌握 py 中时间的基本处理。

课件

3.pdf

代码汇总

  1. 0.1 + 0.2 == 0.3 # False
  2. round(0.1 + 0.2, 1) == 0.3 # True
  3. (0.1 + 0.2) - 0.3 < 0.0001 # True
  1. dayup = pow(1.001, 365)
  2. daydown = pow(0.999, 365)
  3. print("向上:{:.2f},向下:{:.2f}".format(dayup, daydown))
  4. # 向上:1.44,向下:0.69
  1. dayfactor = ???
  2. dayup = pow(1 + dayfactor, 365)
  3. daydown = pow(1 - dayfactor, 365)
  4. print("向上:{:.2f},向下:{:.2f}".format(dayup, daydown))
  5. # dayfactor = 0.005
  6. # 向上:6.17,向下:0.16
  7. # dayfactor = 0.01
  8. # 向上:37.78,向下:0.03
  9. # dayfactor = 0.019
  10. # 向上:962.89,向下:0.00
  1. dayup = 1.0
  2. dayfactor = 0.01
  3. for i in range(365):
  4. if i % 7 in [6, 0]:
  5. dayup = dayup * (1 - dayfactor)
  6. else:
  7. dayup = dayup * (1 + dayfactor)
  8. print("工作日的力量:{:.2f} ".format(dayup))
  9. # 工作日的力量:4.63
  1. def dayUP(df):
  2. dayup = 1
  3. for i in range(365):
  4. if i % 7 in [6, 0]:
  5. dayup = dayup * (1 - 0.01)
  6. else:
  7. dayup = dayup * (1 + df)
  8. return dayup
  9. dayfactor = 0.01
  10. while dayUP(dayfactor) < 37.78: # 37.78 是一年每天努力 1% 的结果
  11. dayfactor += 0.001
  12. print("工作日的努力参数是:{:.3f} ".format(dayfactor))
  13. # 工作日的努力参数是:0.019
  1. # 1. 单引号
  2. '字符串'
  3. # 2. 双引号
  4. "字符串"
  5. # 3. 3 单引号
  6. '''字符串'''
  7. # 4. 3 双引号
  8. """字符串"""
  1. weekStr = "星期一星期二星期三星期四星期五星期六星期日"
  2. weekId = eval(input("请输入星期数字(1-7):"))
  3. pos = (weekId - 1) * 3
  4. print(weekStr[pos : pos + 3])
  5. """
  6. 请输入星期数字(1-7):3
  7. 星期三
  8. """
  1. weekStr = "一二三四五六日"
  2. weekId = eval(input("请输入星期数字(1-7):"))
  3. print("星期" + weekStr[weekId - 1])
  4. """
  5. 请输入星期数字(1-7):3
  6. 星期三
  7. """
  1. print("1 + 1 = 2 " + chr(10004))
  2. print("这个字符♉的Unicode值是:" + str(ord("♉")))
  3. for i in range(12):
  4. print(chr(9800 + i), end="")
  5. """
  6. 1 + 1 = 2 ✔
  7. 这个字符♉的Unicode值是:9801
  8. ♈♉♊♋♌♍♎♏♐♑♒♓
  9. """
  1. print("{0:=^20}".format("PYTHON"))
  2. # =======PYTHON=======
  3. print("{0:*>20}".format("BIT"))
  4. # *****************BIT
  5. print("{:10}".format("BIT"))
  6. # BIT
  7. print("{0:,.2f}".format(12345.6789))
  8. # 12,345.68
  9. print("{0:b},{0:c},{0:d},{0:o},{0:x},{0:X}".format(425))
  10. # 110101001,Ʃ,425,651,1a9,1A9
  11. print("{0:e},{0:E},{0:f},{0:%}".format(3.14))
  12. # 3.140000e+00,3.140000E+00,3.140000,314.000000%
  1. import time
  2. scale = 10
  3. print("------执行开始------")
  4. for i in range(scale + 1):
  5. a = "*" * i
  6. b = "." * (scale - i)
  7. c = (i / scale) * 100
  8. print("{:^3.0f}%[{}->{}]".format(c, a, b))
  9. time.sleep(0.1)
  10. print("------执行结束------")
  11. """
  12. ------执行开始------
  13. 0 %[->..........]
  14. 10 %[*->.........]
  15. 20 %[**->........]
  16. 30 %[***->.......]
  17. 40 %[****->......]
  18. 50 %[*****->.....]
  19. 60 %[******->....]
  20. 70 %[*******->...]
  21. 80 %[********->..]
  22. 90 %[*********->.]
  23. 100%[**********->]
  24. ------执行结束------
  25. """
  1. import time
  2. for i in range(101):
  3. print("\r{:3}%".format(i), end="") # 关键 \r
  4. time.sleep(0.1)
  5. # 最终的打印结果是从 0% 开始,每间隔 0.1s 自增 1%,逐步自增到 100%
  1. import time
  2. scale = 50
  3. print("执行开始".center(scale // 2, "-"))
  4. start = time.perf_counter()
  5. for i in range(scale + 1):
  6. a = "*" * i
  7. b = "." * (scale - i)
  8. c = (i / scale) * 100
  9. dur = time.perf_counter() - start
  10. print("\r{:^3.0f}%[{}->{}]{:.2f}s".format(c, a, b, dur), end="")
  11. time.sleep(0.1)
  12. print("\n" + "执行结束".center(scale // 2, "-"))
  1. name = "Python语言程序设计课程"
  2. print(name[0], name[2:-2], name[-1])
  3. # P thon语言程序设计 程
  1. s = "PYTHON"
  2. print("{0:3}".format(s))
  3. # PYTHON
  1. a = eval(input())
  2. print("{:-^20}".format(pow(a, 3)))
  3. """
  4. 10
  5. --------1000--------
  6. """
  1. n = eval(input())
  2. for i in range(1, n + 1, 2):
  3. print("{0:^{1}}".format("*" * i, n))
  4. """
  5. 3
  6. *
  7. ***
  8. 5
  9. *
  10. ***
  11. *****
  12. 9
  13. *
  14. ***
  15. *****
  16. *******
  17. *********
  18. """
  1. s = input()
  2. t = ""
  3. for c in s:
  4. if "a" <= c <= "z":
  5. t += chr(ord("a") + ((ord(c) - ord("a")) + 3) % 26)
  6. elif "A" <= c <= "Z":
  7. t += chr(ord("A") + ((ord(c) - ord("A")) + 3) % 26)
  8. else:
  9. t += c
  10. print(t)
  11. """
  12. python is good
  13. sbwkrq lv jrrg
  14. """
  1. a = eval(input())
  2. print("{:+>30.3f}".format(pow(a, 0.5)))
  3. """
  4. 10
  5. +++++++++++++++++++++++++3.162
  6. """
  1. s = input()
  2. ls = s.split("-")
  3. print("{}+{}".format(ls[0], ls[-1]))
  4. """
  5. Alice-Bob-Charis-David-Eric-Flurry
  6. Alice+Flurry
  7. """

导学

课前复习

[4.1.1]--前课复习(1).mp4 (158.21MB).mp4%22%2C%22size%22%3A165900206%2C%22taskId%22%3A%22u2e7c5ce1-00e2-4fb4-abab-dc40ae95258%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688200110745-b7e94715-a9f1-4b6a-bc65-774119378127.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22bebD0%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#bebD0) Python 基本语法元素:

  • 缩进、注释、命名、变量、保留字
  • 数据类型、字符串、 整数、浮点数、列表
  • 赋值语句、分支语句、函数
  • input()、print()、eval()、 print() 格式化

Python 基本图形绘制:

  • 从计算机技术演进角度看待Python语言
  • 海龟绘图体系及import保留字用法
  • penup()、pendown()、pensize()、pencolor()
  • fd()、circle()、seth()
  • 循环语句:for和in、range() 函数

本节概要

[4.1.2]--本课概要(1).mp4 (25.92MB).mp4%22%2C%22size%22%3A27176881%2C%22taskId%22%3A%22u076c463a-c8ce-4754-8196-425c2487ed9%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688199982607-9f12d794-1ec5-4ceb-a703-c106780bac91.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22OTRr4%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#OTRr4) 初步学会如何“玩”数字,“玩”字符串

数字类型及操作

在数字类型中,整数和浮点数是重点。
复数类型使用相对较少,和复数相关的知识点直接 pass 掉先,在本周的后续内容中也不会涉及到复数。
数值运算操作符是重点内容,但其中提及的二元操作符并不是很重要,只要能够理解它的等价写法即可。
数值运算函数也是重点,不过对于 API 还是那句话,能够看懂就直接过,后续接触到多了自然就记住了。

单元开篇

[4.2.1]--单元开篇(1).mp4 (8.22MB).mp4%22%2C%22size%22%3A8620610%2C%22taskId%22%3A%22u5fc569ec-62ea-4c38-ae87-db54d686528%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688200059769-60449975-28c2-4328-9396-accf2e0bcb52.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22ZLE5V%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#ZLE5V)

整数类型

[4.2.2]--整数类型(1).mp4 (33.88MB).mp4%22%2C%22size%22%3A35522624%2C%22taskId%22%3A%22u2747af5d-ebfb-49fd-91fb-20964dd9bcf%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688200059754-92faf32c-fc02-49a6-a925-c4ffce845d96.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22VPUmG%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#VPUmG)

  • 整数类型:与数学中整数的概念一致
  • Python 中的整数可正可负
  • Python 中的整数没有范围限制
  • pow(x, y) 函数,计算 xy,想算多大算多大
  • 整数类型的 4 种进制表示形式
    • 十进制:1010 99 -217
    • 二进制,以 0b0B 开头:0b010 -0B101
    • 八进制,以 0o0O 开头:0o123 -0O456
    • 十六进制,以 0x0X 开头:0x9a -0X89

浮点数类型

[4.2.3]--浮点数类型(1).mp4 (95.65MB).mp4%22%2C%22size%22%3A100299652%2C%22taskId%22%3A%22u2423c474-9f75-4ba3-97f2-d95ab5365d6%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688200059765-02115da3-7458-45ff-99ef-6aa9b36d494d.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22Man8H%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#Man8H)

  • 浮点数类型:与数学中实数的概念一致,即带有小数点及小数的数字
  • Python 中的浮点数的取值范围和小数精度都存在限制,但常规计算可忽略
    • 取值范围数量级约-10307至10308
    • 精度数量级10-16
  • 不确定尾数:浮点数间运算存在不确定尾数,不是 bug
    image.png
  • 科学计数法:**<a>e<b>** 表示 **a*10****b**,例如:4.3e-3 值为 0.00439.6E5 值为 960000.0
  • 浮点数的取值范围和精度基本无限制
  • round(x, d):对 x 四舍五入,d 是小数截取位数
    • 浮点数间运算与比较用round()函数辅助,学会使用 **round()** 函数解决浮点数计算失真导致的问题
    • 不确定尾数一般发生在10-16左右,round()十分有效
  1. 0.1 + 0.2 == 0.3 # False
  2. round(0.1 + 0.2, 1) == 0.3 # True

其实有很多法子解决浮点数计算失真的问题,并不一定非得使用 round 函数,比如我们可以通过作差比较两个浮点数是否相等,这样可以避免直接比较两个浮点数,而是比较他们的差的绝对值是否小于一个非常小的数

  1. (0.1 + 0.2) - 0.3 < 0.0001 # True

复数类型

[4.2.4]--复数类型(1).mp4 (36.15MB).mp4%22%2C%22size%22%3A37901156%2C%22taskId%22%3A%22u473f8ee0-70c8-4874-9aee-5f7e0624fda%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688200059764-87a5759e-7e20-45e3-aa69-c4053632f10d.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22TalDe%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#TalDe) 复数类型:与数学中复数的概念一致

日常开发中很少情况下会用到这种类型,老师表示课程中也仅仅是对这种类型做一个简单的介绍,让我们知道有这种类型,在后续内容中并不会提供具体的实例来介绍它。

勘误:只有 python 中有复数类型的说法是错误的 👉🏻 复数类型是 python 特有的嘛?

数值运算操作符

[4.2.5]--数值运算操作符(1).mp4 (80.52MB).mp4%22%2C%22size%22%3A84435479%2C%22taskId%22%3A%22ud1c61b33-fe39-4227-819d-103b4924471%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688200059768-7c34b7f2-1014-4b17-9b3a-89cf5bade26b.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22wR9G8%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#wR9G8) 理解操作符:操作符是完成运算的一种符号体系

image.png

注意:/// 操作符,它们和其它编程语言差异较大的地方

image.png

二元运算符:
image.png

数字类型的关系

  • 类型间可进行混合运算,生成结果为”最宽”类型
  • 三种类型存在一种逐渐”扩展”或”变宽”的关系:整数 -> 浮点数 -> 复数

数值运算函数

[4.2.6]--数值运算函数(1).mp4 (85.41MB).mp4%22%2C%22size%22%3A89555760%2C%22taskId%22%3A%22uba958c9f-b87c-442d-b486-ac84a8ad4de%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688200139723-3748ffd1-d128-420d-bc72-242afc8ca061.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22pmLZy%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#pmLZy)

image.png
image.png
image.png

单元小结

[4.2.7]--单元小结(1).mp4 (15.86MB).mp4%22%2C%22size%22%3A16632914%2C%22taskId%22%3A%22u8310e25d-adba-41d6-aa69-5369aecddac%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688200188889-48029b40-9987-4591-82b6-9695cd7e2253.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22inXqn%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#inXqn)

  • 整数类型的无限范围及 4 种进制表示
  • 浮点数类型的近似无限范围、小尾数及科学计数法
  • + - * / // % ** 二元增强赋值操作符
  • abs()、divmod()、pow()、round()、max()、min()、int()、float()、complex()

实例 3: 天天向上的力量

理解 4 个问题,掌握 4 个问题的求解。
认识使用变量的意义。
第四问中涉及到了函数的相关内容,函数会在第五周介绍,先对函数的写法和作用有个初步的认识即可。

问题分析

[4.3.1]--天天向上的力量问题分析(1).mp4 (28.5MB).mp4%22%2C%22size%22%3A29881764%2C%22taskId%22%3A%22u1a7e7d33-9dc7-448f-bcaa-c83e55d96ed%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688432931543-841224db-b221-4465-898f-8880fa7c6d17.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22PahVw%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#PahVw) 原来“好好学习,天天向上”这句话出处是……
image.png

第一问

[4.3.2]--天天向上的力量第一问(1).mp4 (31.58MB).mp4%22%2C%22size%22%3A33109526%2C%22taskId%22%3A%22ub82d8bb6-c627-481a-b4f9-c221ce668d2%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688432931563-26caa57c-dae5-47f7-82b8-06ffb5693404.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22OmfYp%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#OmfYp) image.png

  1. dayup = pow(1.001, 365)
  2. daydown = pow(0.999, 365)
  3. print("向上:{:.2f},向下:{:.2f}".format(dayup, daydown))
  4. # 向上:1.44,向下:0.69

第二问

[4.3.3]--天天向上的力量第二问(1).mp4 (36.81MB).mp4%22%2C%22size%22%3A38595107%2C%22taskId%22%3A%22uaeb816ee-b1e0-48d3-a1c8-907bfbe0bf5%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688432931565-6b59bc26-b93c-4f2d-8ea9-a19405dbdfc4.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22F79dx%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#F79dx) image.png

  1. dayfactor = ???
  2. dayup = pow(1 + dayfactor, 365)
  3. daydown = pow(1 - dayfactor, 365)
  4. print("向上:{:.2f},向下:{:.2f}".format(dayup, daydown))
  5. # dayfactor = 0.005
  6. # 向上:6.17,向下:0.16
  7. # dayfactor = 0.01
  8. # 向上:37.78,向下:0.03
  9. # dayfactor = 0.019
  10. # 向上:962.89,向下:0.00

通过此 demo,体会变量的好处 —— 一处修改即可

第三问

[4.3.4]--天天向上的力量第三问(1).mp4 (86.15MB).mp4%22%2C%22size%22%3A90332088%2C%22taskId%22%3A%22u9518ee35-6bd1-47f4-b8e0-5b19a36ecd9%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688432931562-1699eacd-a677-4e4d-a690-a5aa4365ecc1.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22oWlKz%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#oWlKz) image.png

  1. dayup = 1.0
  2. dayfactor = 0.01
  3. for i in range(365):
  4. if i % 7 in [6, 0]:
  5. dayup = dayup * (1 - dayfactor)
  6. else:
  7. dayup = dayup * (1 + dayfactor)
  8. print("工作日的力量:{:.2f} ".format(dayup))
  9. # 工作日的力量:4.63

第四问

[4.3.5]--天天向上的力量第四问(1).mp4 (120.48MB).mp4%22%2C%22size%22%3A126335242%2C%22taskId%22%3A%22u464fb1f9-8726-4966-8377-014f56ab0be%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688432931571-7caed80a-e2bb-4fee-9939-3b1b4d34013f.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22m89y6%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#m89y6) image.png
问:B 君在工作日需要每天进步多少,才能达到 A 君的效果呢?

image.png
image.png

  1. def dayUP(df):
  2. dayup = 1
  3. for i in range(365):
  4. if i % 7 in [6, 0]:
  5. dayup = dayup * (1 - 0.01)
  6. else:
  7. dayup = dayup * (1 + df)
  8. return dayup
  9. dayfactor = 0.01
  10. while dayUP(dayfactor) < 37.78: # 37.78 是一年每天努力 1% 的结果
  11. dayfactor += 0.001
  12. print("工作日的努力参数是:{:.3f} ".format(dayfactor))
  13. # 工作日的努力参数是:0.019

:::success image.png
现在时间:image.png
看完本节后,突然想起昨晚看的一部番《跃动青春》里的一句台词…… :::

举一反三

[4.3.6]--天天向上的力量举一反三(1).mp4 (49.09MB).mp4%22%2C%22size%22%3A51477731%2C%22taskId%22%3A%22u0f4f236e-871b-40ab-b18b-59ad38baff6%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688432947784-aceb6e0c-ee99-4c60-aa8a-2c34ad425996.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22LqYj1%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#LqYj1) 本节介绍的天天向上实例,虽然仅包含 8~12 行代码,但包含很多语法元素

  • 条件循环
  • 计数循环
  • 分支
  • 函数
  • 计算思维

清楚理解这些代码能够快速入门 Python 语言

字符串类型及操作

理解 python 中的多行注释的本质就是字符串。
掌握字符串的 4 种写法 '字符串' '''字符串'''``"字符串" """字符串"""
理解字符串的序号,并掌握字符串索引和切片的使用。
了解转义符的作用。

掌握字符串的操作符

  • + 拼接
  • * 复制
  • in 判断子串

能够看懂本节提及的字符串相关的 API。

字符串的格式化也是重点内容,不过细节有些多 —— “槽”的“格式控制标记”的写法。
可以结合老师的提供的若干示例来理解字符串的格式化,先理解课程中提供的示例代码。

单元开篇

[4.4.1]--单元开篇(1).mp4 (17.12MB).mp4%22%2C%22size%22%3A17948535%2C%22taskId%22%3A%22uae6d2cac-0654-4f48-897f-ee324b1fe17%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688432962325-dd7761f6-2492-4b84-a7c2-9fb6534a9d0f.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22oMt2b%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#oMt2b)

字符串类型的表示

[4.4.2]--字符串类型的表示(1).mp4 (128.24MB).mp4%22%2C%22size%22%3A134472864%2C%22taskId%22%3A%22uc065e600-e764-43a6-b4f5-bb1a2762998%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688432962339-ce82e746-d502-448e-aa91-14acdf6f5e7f.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22TQILG%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#TQILG) 在 python 语言中,并没有提供多行注释的表示方式
3 单引号或双引号构成的就是字符串,并非注释,但它可以当做程序的注释来使用。
若在程序中出现了一个字符串,但是这个字符串并没有给到一个变量中或者这个字符串没有用于任何操作,那么它也可以当做注释来使用。

字符串有 2 类共 4 种 表示方法:

  1. # 1. 单引号
  2. '字符串'
  3. # 2. 双引号
  4. "字符串"
  5. # 3. 3 单引号
  6. '''字符串'''
  7. # 4. 3 双引号
  8. """字符串"""

image.png

字符串的序号:
image.png

字符串的使用:索引、切片
image.png
image.png

转义符:
image.png
其中 \r 在“实例 4:文本进度条”中会用到

字符串操作符

[4.4.3]--字符串操作符(1).mp4 (56.16MB).mp4%22%2C%22size%22%3A58889864%2C%22taskId%22%3A%22u1d78e01c-e164-460e-bd6e-c3704bcb395%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688432962350-d48ba1b9-99bc-4d47-bfae-8a6e07898bf6.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22kjZd6%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#kjZd6) image.png

练习:
image.png

  1. weekStr = "星期一星期二星期三星期四星期五星期六星期日"
  2. weekId = eval(input("请输入星期数字(1-7):"))
  3. pos = (weekId - 1) * 3
  4. print(weekStr[pos : pos + 3])
  5. """
  6. 请输入星期数字(1-7):3
  7. 星期三
  8. """
  1. weekStr = "一二三四五六日"
  2. weekId = eval(input("请输入星期数字(1-7):"))
  3. print("星期" + weekStr[weekId - 1])
  4. """
  5. 请输入星期数字(1-7):3
  6. 星期三
  7. """

字符串处理函数

[4.4.4]--字符串处理函数(1).mp4 (117.03MB).mp4%22%2C%22size%22%3A122709962%2C%22taskId%22%3A%22u0320494f-0bee-4f13-87d6-5bed6f4df81%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688432962564-fa7600b1-76ab-4378-8b13-36f2fcd9f05c.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22rshj9%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#rshj9) 注意:一个字符无论是数字、中文字符、英文字符…… 它们在 python 中的长度都是 1

image.png
与 str 对应的是 eval 函数

image.png
image.png

Python 字符串的编码方式:Unicode 编码

Unicode 编码:

  • 统一字符编码,即覆盖几乎所有字符的编码方式
  • 从0到1114111 (0x10FFFF)空间,每个编码对应一个字符
  • Python字符串中每个字符都是Unicode编码字符
  1. print("1 + 1 = 2 " + chr(10004))
  2. print("这个字符♉的Unicode值是:" + str(ord("♉")))
  3. for i in range(12):
  4. print(chr(9800 + i), end="")
  5. """
  6. 1 + 1 = 2 ✔
  7. 这个字符♉的Unicode值是:9801
  8. ♈♉♊♋♌♍♎♏♐♑♒♓
  9. """

:::warning 【补充说明】
image.png
准确点儿来说,这张图中提到的 Unicode 指的是“Unicode 码点值”

🤔 什么是“Unicode 编码”、“Unicode 码点值”呢?

  • Unicode 编码:是一种字符编码方案,用于表示和处理字符
  • Unicode 码点值:是字符在 Unicode 字符集中的唯一整数值

两者之间的关系:Unicode 编码定义了字符和对应的码点值之间的映射关系

我们可以这么理解:一个字符对应一个唯一的数字

  • chr() 可以根据我们给定的一个数字,找到对应的字符
  • ord() 可以根据我们给定的一个字符,找到对应的数字

🤔 chr、ord 的全称是?

  • chr 的全称是 character 表示字符
  • ord 的全称是 ordinal 表示顺序、序数 :::

字符串处理方法

[4.4.5]--字符串处理方法(1).mp4 (90.18MB).mp4%22%2C%22size%22%3A94558656%2C%22taskId%22%3A%22ufbc34c09-dee3-4aa4-a5ef-7f16fd1c1a1%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688432972638-b0417dda-cf64-435c-b9d9-7effacb1d381.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22MrIFX%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#MrIFX) 理解“方法”:
image.png
对于方法和函数的概念,在我看来都是一个意思,都是用函数调用符 () 来调用的,没必要区分那么细。

image.png
image.png
image.png

字符串类型的格式化

[4.4.6]--字符串类型的格式化(1).mp4 (142.4MB).mp4%22%2C%22size%22%3A149315258%2C%22taskId%22%3A%22uc2c3c853-9d51-49ff-886c-dfcaab7d8f0%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688432972730-a76b1402-31dd-4cf3-8500-d22d57e5d637.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22vfrCO%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#vfrCO)

  • 格式化是对字符串进行格式表达的方式
  • 字符串格式化使用 .format()方法,用法:<模板字符串>.format(<逗号分隔的参数>)
    • 相当于一个占位符
    • 使用一对大括号 {} 表示
    • 仅在字符串中可用

image.png
image.png

image.png

image.png

image.png

  1. print("{0:=^20}".format("PYTHON"))
  2. # =======PYTHON=======
  3. print("{0:*>20}".format("BIT"))
  4. # *****************BIT
  5. print("{:10}".format("BIT"))
  6. # BIT
  7. print("{0:,.2f}".format(12345.6789))
  8. # 12,345.68
  9. print("{0:b},{0:c},{0:d},{0:o},{0:x},{0:X}".format(425))
  10. # 110101001,Ʃ,425,651,1a9,1A9
  11. print("{0:e},{0:E},{0:f},{0:%}".format(3.14))
  12. # 3.140000e+00,3.140000E+00,3.140000,314.000000%

单元小结

[4.4.7]--单元小结(1).mp4 (13.95MB).mp4%22%2C%22size%22%3A14631896%2C%22taskId%22%3A%22ubcefc503-3278-4bba-b8e0-9d8a691d5d2%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688432992879-648634b6-9649-415f-9a71-7869b043affc.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22yLF1d%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#yLF1d)

  • 正向递增序号、反向递减序号、<字符串>[M:N:K]
  • +、*、in、len()、str()、hex()、oct()、ord()、chr()
  • .lower()、.upper()、.split()、.count()、.replace()
  • .center()、.strip()、.join() 、.format()格式化

模块 2: time 库的使用

本节仅仅介绍的 API 的功能,并没有提供示例来加以解释,全节听下来有那么点儿字典的味道……
对于视频中提及的 API 介绍,都截了图,快速过一遍,看懂直接 pass 掉即可。
若写代码时遇到和时间相关的逻辑,尝试使用 time 库来解决,到时若记不住 time 库的使用,再查阅 time 库的相关 API 即可。

对于 time 库的使用,主要会在实例 4 文本进度条中加以应用。

单元开篇

[4.5.1]--单元开篇(1).mp4 (5.08MB).mp4%22%2C%22size%22%3A5325793%2C%22taskId%22%3A%22uf0fc7127-28d9-4b79-a19c-63e43b6fd01%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688433004940-560dd384-4a7c-44d7-937c-a5ac7462755b.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22YYjap%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#YYjap)

time 库基本介绍

[4.5.2]--time库基本介绍(1).mp4 (19.65MB).mp4%22%2C%22size%22%3A20607123%2C%22taskId%22%3A%22ua049cca8-4a15-4428-ac71-8a2afe22027%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688433012125-4c5bc79d-09b9-49b2-9fb4-a53f99d38284.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22JFEJ2%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#JFEJ2) time 库是 Python 中处理时间的标准库

  • 计算机时间的表达
  • 提供获取系统时间并格式化输出功能
  • 提供系统级精确计时功能,用于程序性能分析
  1. import time
  2. time.<b>()

小结:time 库包括三类函数

  • 时间获取:time() ctime() gmtime()
  • 时间格式化:strftime() strptime()
  • 程序计时:sleep() perf_counter()

时间获取

[4.5.3]--时间获取(1).mp4 (48.12MB).mp4%22%2C%22size%22%3A50459198%2C%22taskId%22%3A%22u9492ede4-f6bb-4bdf-8bec-a0ec3e3d1b1%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688433012660-4c270f97-7a4c-4c64-a9ef-55adb9959ae1.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22fSFrD%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#fSFrD) image.png
image.png

时间格式化

[4.5.4]--时间格式化(1).mp4 (111.27MB).mp4%22%2C%22size%22%3A116673974%2C%22taskId%22%3A%22u444f7a71-4299-4a33-8b7c-6d28ebee1f3%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688433012701-9b614406-fc89-40ee-b8b8-c8b853d6a448.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22U2ztZ%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#U2ztZ) 时间格式化:将时间以合理的方式展示出来
image.png
image.png
image.png
image.png
image.png

程序计时应用

[4.5.5]--程序计时应用(1).mp4 (39.56MB).mp4%22%2C%22size%22%3A41483315%2C%22taskId%22%3A%22u06c9b5cf-457b-400e-be8f-9f31539e634%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688433013613-599710b8-471d-46fd-b255-b082af31f5f5.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22kPTsw%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#kPTsw)

  • 程序计时指测量起止动作所经历时间的过程
  • 测量时间:perf_counter()
  • 产生时间:sleep()

image.png
其中 perf 是 performance(性能) 的缩写,perf_counter 可以理解为“性能测试”、“性能计时”。
理解了 perf_counter 的含义后,不难认识到它的作用:测量代码执行时间和性能的计时器。

image.png

上述两个 API:time.perf_counter() 和 time.sleep(s) 在接下来的文本进度条实例中会用到。

单元小结

[4.5.6]--单元小结(1).mp4 (17.31MB).mp4%22%2C%22size%22%3A18150275%2C%22taskId%22%3A%22uf87d6164-91ec-481a-8302-f55a5c65fb0%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688433022986-de74f207-62cf-4ae2-a42e-9c30904cc8a6.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22ldHvx%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#ldHvx)

  • 时间获取:time() ctime() gmtime()
  • 时间格式化:strftime() strptime()
  • 程序计时:sleep() perf_counter()

实例 4: 文本进度条

问题分析

[4.6.1]--文本进度条问题分析(1).mp4 (15.56MB).mp4%22%2C%22size%22%3A16317809%2C%22taskId%22%3A%22u06805ea8-2b1c-4754-b48d-45daa1ef808%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688433032533-1c293ee5-eaf9-4eca-bd08-d5df62443aaa.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22e7MDh%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#e7MDh) 文本进度条:

  • 采用字符串方式打印可以动态变化的文本进度条
  • 进度条需要能在一行中逐渐变化

如何获得文本进度条的变化时间?
采用 sleep() 模拟一个持续的进度

“文本进度条”简单的开始

[4.6.2]--文本进度条简单的开始(1).mp4 (63.05MB).mp4%22%2C%22size%22%3A66110948%2C%22taskId%22%3A%22u8c697aba-0a7d-47f8-bc64-ebe987b0b93%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688433032973-87ed10ca-e8b4-4f3c-aac2-245bf2aa452a.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22oL5ug%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#oL5ug)

  1. import time
  2. scale = 10
  3. print("------执行开始------")
  4. for i in range(scale + 1):
  5. a = "*" * i
  6. b = "." * (scale - i)
  7. c = (i / scale) * 100
  8. print("{:^3.0f}%[{}->{}]".format(c, a, b))
  9. time.sleep(0.1)
  10. print("------执行结束------")
  11. """
  12. ------执行开始------
  13. 0 %[->..........]
  14. 10 %[*->.........]
  15. 20 %[**->........]
  16. 30 %[***->.......]
  17. 40 %[****->......]
  18. 50 %[*****->.....]
  19. 60 %[******->....]
  20. 70 %[*******->...]
  21. 80 %[********->..]
  22. 90 %[*********->.]
  23. 100%[**********->]
  24. ------执行结束------
  25. """

“文本进度条”单行动态刷新

[4.6.3]--文本进度条单行动态刷新(1).mp4 (98.71MB).mp4%22%2C%22size%22%3A103508141%2C%22taskId%22%3A%22uf72b103a-eba9-43f2-b034-06a32aee814%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688433033153-41468858-0a11-4269-952f-2a0f07f5fde5.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22JlVGy%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#JlVGy) image.png

  1. import time
  2. for i in range(101):
  3. print("\r{:3}%".format(i), end="")
  4. time.sleep(0.1)
  5. # 最终的打印结果是从 0% 开始,每间隔 0.1s 自增 1%,逐步自增到 100%

“文本进度条”实例完整效果

[4.6.4]--文本进度条实例完整效果(1).mp4 (53.48MB).mp4%22%2C%22size%22%3A56077366%2C%22taskId%22%3A%22u0cc2c8b0-d4bd-474e-94ea-b113dba2c2b%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688433033400-e3dfc18b-45ce-4b71-a4fc-eaad7e321cda.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22zHYKA%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#zHYKA)

  1. import time
  2. scale = 50
  3. print("执行开始".center(scale // 2, "-"))
  4. start = time.perf_counter()
  5. for i in range(scale + 1):
  6. a = "*" * i
  7. b = "." * (scale - i)
  8. c = (i / scale) * 100
  9. dur = time.perf_counter() - start
  10. print("\r{:^3.0f}%[{}->{}]{:.2f}s".format(c, a, b, dur), end="")
  11. time.sleep(0.1)
  12. print("\n" + "执行结束".center(scale // 2, "-"))

举一反三

[4.6.5]--文本进度条举一反三(1).mp4 (83.33MB).mp4%22%2C%22size%22%3A87374050%2C%22taskId%22%3A%22u80847a64-abae-4a1f-beee-8ddc3f9ad55%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688433041984-168d6924-c4e2-48f1-8f59-5a71012650da.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22K4qoz%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#K4qoz)

练习

[4.7.1]--第3周练习与作业(1).mp4 (10.87MB).mp4%22%2C%22size%22%3A11397431%2C%22taskId%22%3A%22u9ad3e07a-33c0-4e5f-90d4-c94e93cbb13%22%2C%22taskType%22%3A%22upload%22%2C%22url%22%3Anull%2C%22cover%22%3Anull%2C%22videoId%22%3A%22inputs%2Fprod%2Fyuque%2F2023%2F2331396%2Fmp4%2F1688433221357-b0ee0b79-5ad3-482e-ba4a-9a455f937bff.mp4%22%2C%22download%22%3Afalse%2C%22__spacing%22%3A%22both%22%2C%22id%22%3A%22Aoxrc%22%2C%22margin%22%3A%7B%22top%22%3Atrue%2C%22bottom%22%3Atrue%7D%2C%22card%22%3A%22video%22%7D#Aoxrc)

单选题

image.png

image.png

image.png

image.png

勘误:最后几个字应该描述的是:val 的“字符”个数,而非“数字”个数。

image.png

image.png

image.png

image.png

image.png

image.png

实例3:天天向上的力量

image.png

  1. def dayUP(df):
  2. dayup = 1
  3. for i in range(365):
  4. if i % 7 in [6,0]:
  5. dayup = dayup*(1 - 0.01)
  6. else:
  7. dayup = dayup*(1 + df)
  8. return dayup
  9. dayfactor = 0.01
  10. while dayUP(dayfactor) < 37.78:
  11. dayfactor += 0.001
  12. print("工作日的努力参数是: {:.3f}".format(dayfactor))
  13. # 工作日的努力参数是: 0.019

这是本课程的实例3:
(1) 工作日模式,每天要努力到1.9%,相当于365模式每天1%的;
(2) 采用{:.3f}将输出数字变成三位小数点表示时,即使数学上该输出值是整数,也会按照小数方式输出。

实例4:文本进度条

image.png

  1. import time
  2. scale = 50
  3. print("执行开始".center(scale // 2, "-"))
  4. start = time.perf_counter()
  5. for i in range(scale + 1):
  6. a = "*" * i
  7. b = "." * (scale - i)
  8. c = (i / scale) * 100
  9. dur = time.perf_counter() - start
  10. print("\r{:^3.0f}%[{}->{}]{:.2f}s".format(c, a, b, dur), end="")
  11. time.sleep(0.1)
  12. print("\n" + "执行结束".center(scale // 2, "-"))
  13. """
  14. -----------执行开始----------
  15. 100%[**************************************************->]5.20s
  16. -----------执行结束----------
  17. """

这是本课程的实例4,请注意三点:
(1) 文本进度条程序使用了 perf_counter() 计时,计时方法适合各类需要统间的算问题,例如:比较不同算法时间 、统计程序运行时
(2) 进度条的单行回退要在命令行(也叫控制台、Windows的cmd)下才能使用,IDLE屏蔽了’\r’的功能
(3) 进度条可应用在:任何运行时间需要较长的程序中;任何希望提高用户体验的应中;进度条是人机交互的纽带之一

三次方格式化

image.png

  1. a = eval(input())
  2. print("{:-^20}".format(pow(a, 3)))
  3. """
  4. 10
  5. --------1000--------
  6. """

这是一个简单题,重点在于理解格式化输出的方法。

星号三角形

image.png

  1. n = eval(input())
  2. for i in range(1, n + 1, 2):
  3. print("{0:^{1}}".format("*" * i, n))
  4. """
  5. 3
  6. *
  7. ***
  8. 5
  9. *
  10. ***
  11. *****
  12. 9
  13. *
  14. ***
  15. *****
  16. *******
  17. *********
  18. """

关键是对.format()中槽机制的理解,槽中可以嵌套槽,用来表示宽度、填充等含义。

凯撒密码

image.png

  1. s = input()
  2. t = ""
  3. for c in s:
  4. if "a" <= c <= "z":
  5. t += chr(ord("a") + ((ord(c) - ord("a")) + 3) % 26)
  6. elif "A" <= c <= "Z":
  7. t += chr(ord("A") + ((ord(c) - ord("A")) + 3) % 26)
  8. else:
  9. t += c
  10. print(t)
  11. """
  12. python is good
  13. sbwkrq lv jrrg
  14. """

恺撒密码加密算法由很多种编写方法,这是一种很直接的方法。

平方根格式化

image.png

  1. a = eval(input())
  2. print("{:+>30.3f}".format(pow(a, 0.5)))
  3. """
  4. 10
  5. +++++++++++++++++++++++++3.162
  6. """

这是一个简单题,重点在于理解格式化输出的方法。

注意:如果平凡根后产生一个复数,由于复数的实部和虚部都是浮点数,.3f可以将实部和虚部分别取三位小数。

字符串分段组合

image.png

  1. s = input()
  2. ls = s.split("-")
  3. print("{}+{}".format(ls[0], ls[-1]))
  4. """
  5. Alice-Bob-Charis-David-Eric-Flurry
  6. Alice+Flurry
  7. """

s.split(k) 以 k 为标记分割 s,产生一个列表。通过该题目,掌握 split() 方法的使用,注意:k 可以是单字符,也可以是字符串。

补充

计算机中小数计算存在不确定尾数的原因是什么?

计算机在计算浮点数时的失真问题
在学习不同的编程语言时,老师们普遍会提到的一个问题。以这篇文档记录一下自己对此的理解。

复数类型是 python 特有的嘛?

不是的,复数类型不是Python特有的。许多编程语言都支持复数类型,包括但不限于Python、C++、Java、Fortran、Ruby等。这些语言中,复数的表示和操作可能略有不同,但基本的概念是一致的。

在Python中,复数类型是内建的,可以直接创建和操作复数。例如:

  1. x = 1 + 2j # 创建复数
  2. y = 2 + 3j
  3. z = x + y # 复数的加法

在其他一些语言中,如 C++ 或Java,可能需要通过特定的库或者使用对象来实现复数的功能。例如在 C++ 中,复数是通过 <complex> 库提供的 std::complex 类来实现的。

因此,虽然复数类型在很多语言中都有,但其使用的具体语法和实现方式会因语言而异。

Python 中的整数有范围限制吗?

Python 中的整数在理论上是没有大小限制的,只受限于你的机器的内存大小

在大多数语言中,整数都是有固定的大小的,例如在 Java 或者 C++ 中,整数的大小一般是32位或64位。这意味着整数的大小有上限和下限,如果超过这个范围,就会发生溢出。

在 Python 中,整数的大小是动态的,可以根据需要自动扩展。这使得 Python 非常适合于需要大整数计算的应用,例如密码学或者大数计算。

然而,这种灵活性也有一个代价。Python 的整数使用的内存比其他语言的整数多,因为它需要存储额外的信息来管理这种动态大小。而且,因为整数的大小可以无限扩展,如果你创建了一个非常大的整数,可能会消耗大量的内存,甚至导致你的程序崩溃。

所以,虽然 Python 的整数没有大小限制,但在实际应用中,你还是需要注意管理你的内存使用,避免创建过大的整数。