简述
介绍 Python 中的两种基本数据类型:
- 数字类型(整数、浮点数和复数)
- 字符串类型
介绍 time 库的基本使用,掌握 py 中时间的基本处理。
课件
代码汇总
0.1 + 0.2 == 0.3 # False
round(0.1 + 0.2, 1) == 0.3 # True
(0.1 + 0.2) - 0.3 < 0.0001 # True
dayup = pow(1.001, 365)
daydown = pow(0.999, 365)
print("向上:{:.2f},向下:{:.2f}".format(dayup, daydown))
# 向上:1.44,向下:0.69
dayfactor = ???
dayup = pow(1 + dayfactor, 365)
daydown = pow(1 - dayfactor, 365)
print("向上:{:.2f},向下:{:.2f}".format(dayup, daydown))
# dayfactor = 0.005
# 向上:6.17,向下:0.16
# dayfactor = 0.01
# 向上:37.78,向下:0.03
# dayfactor = 0.019
# 向上:962.89,向下:0.00
dayup = 1.0
dayfactor = 0.01
for i in range(365):
if i % 7 in [6, 0]:
dayup = dayup * (1 - dayfactor)
else:
dayup = dayup * (1 + dayfactor)
print("工作日的力量:{:.2f} ".format(dayup))
# 工作日的力量:4.63
def dayUP(df):
dayup = 1
for i in range(365):
if i % 7 in [6, 0]:
dayup = dayup * (1 - 0.01)
else:
dayup = dayup * (1 + df)
return dayup
dayfactor = 0.01
while dayUP(dayfactor) < 37.78: # 37.78 是一年每天努力 1% 的结果
dayfactor += 0.001
print("工作日的努力参数是:{:.3f} ".format(dayfactor))
# 工作日的努力参数是:0.019
# 1. 单引号
'字符串'
# 2. 双引号
"字符串"
# 3. 3 单引号
'''字符串'''
# 4. 3 双引号
"""字符串"""
weekStr = "星期一星期二星期三星期四星期五星期六星期日"
weekId = eval(input("请输入星期数字(1-7):"))
pos = (weekId - 1) * 3
print(weekStr[pos : pos + 3])
"""
请输入星期数字(1-7):3
星期三
"""
weekStr = "一二三四五六日"
weekId = eval(input("请输入星期数字(1-7):"))
print("星期" + weekStr[weekId - 1])
"""
请输入星期数字(1-7):3
星期三
"""
print("1 + 1 = 2 " + chr(10004))
print("这个字符♉的Unicode值是:" + str(ord("♉")))
for i in range(12):
print(chr(9800 + i), end="")
"""
1 + 1 = 2 ✔
这个字符♉的Unicode值是:9801
♈♉♊♋♌♍♎♏♐♑♒♓
"""
print("{0:=^20}".format("PYTHON"))
# =======PYTHON=======
print("{0:*>20}".format("BIT"))
# *****************BIT
print("{:10}".format("BIT"))
# BIT
print("{0:,.2f}".format(12345.6789))
# 12,345.68
print("{0:b},{0:c},{0:d},{0:o},{0:x},{0:X}".format(425))
# 110101001,Ʃ,425,651,1a9,1A9
print("{0:e},{0:E},{0:f},{0:%}".format(3.14))
# 3.140000e+00,3.140000E+00,3.140000,314.000000%
import time
scale = 10
print("------执行开始------")
for i in range(scale + 1):
a = "*" * i
b = "." * (scale - i)
c = (i / scale) * 100
print("{:^3.0f}%[{}->{}]".format(c, a, b))
time.sleep(0.1)
print("------执行结束------")
"""
------执行开始------
0 %[->..........]
10 %[*->.........]
20 %[**->........]
30 %[***->.......]
40 %[****->......]
50 %[*****->.....]
60 %[******->....]
70 %[*******->...]
80 %[********->..]
90 %[*********->.]
100%[**********->]
------执行结束------
"""
import time
for i in range(101):
print("\r{:3}%".format(i), end="") # 关键 \r
time.sleep(0.1)
# 最终的打印结果是从 0% 开始,每间隔 0.1s 自增 1%,逐步自增到 100%
import time
scale = 50
print("执行开始".center(scale // 2, "-"))
start = time.perf_counter()
for i in range(scale + 1):
a = "*" * i
b = "." * (scale - i)
c = (i / scale) * 100
dur = time.perf_counter() - start
print("\r{:^3.0f}%[{}->{}]{:.2f}s".format(c, a, b, dur), end="")
time.sleep(0.1)
print("\n" + "执行结束".center(scale // 2, "-"))
name = "Python语言程序设计课程"
print(name[0], name[2:-2], name[-1])
# P thon语言程序设计 程
s = "PYTHON"
print("{0:3}".format(s))
# PYTHON
a = eval(input())
print("{:-^20}".format(pow(a, 3)))
"""
10
--------1000--------
"""
n = eval(input())
for i in range(1, n + 1, 2):
print("{0:^{1}}".format("*" * i, n))
"""
3
*
***
5
*
***
*****
9
*
***
*****
*******
*********
"""
s = input()
t = ""
for c in s:
if "a" <= c <= "z":
t += chr(ord("a") + ((ord(c) - ord("a")) + 3) % 26)
elif "A" <= c <= "Z":
t += chr(ord("A") + ((ord(c) - ord("A")) + 3) % 26)
else:
t += c
print(t)
"""
python is good
sbwkrq lv jrrg
"""
a = eval(input())
print("{:+>30.3f}".format(pow(a, 0.5)))
"""
10
+++++++++++++++++++++++++3.162
"""
s = input()
ls = s.split("-")
print("{}+{}".format(ls[0], ls[-1]))
"""
Alice-Bob-Charis-David-Eric-Flurry
Alice+Flurry
"""
导学
课前复习
.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() 函数
本节概要
.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 还是那句话,能够看懂就直接过,后续接触到多了自然就记住了。
单元开篇
.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)
整数类型
.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
- 二进制,以
0b
或0B
开头:0b010
-0B101
- 八进制,以
0o
或0O
开头:0o123
-0O456
- 十六进制,以
0x
或0X
开头:0x9a
-0X89
- 十进制:
浮点数类型
.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
- 科学计数法:
**<a>e<b>**
表示**a*10****b**
,例如:4.3e-3
值为0.0043
,9.6E5
值为960000.0
- 浮点数的取值范围和精度基本无限制
round(x, d)
:对 x 四舍五入,d 是小数截取位数- 浮点数间运算与比较用round()函数辅助,学会使用
**round()**
函数解决浮点数计算失真导致的问题 - 不确定尾数一般发生在10-16左右,round()十分有效
- 浮点数间运算与比较用round()函数辅助,学会使用
0.1 + 0.2 == 0.3 # False
round(0.1 + 0.2, 1) == 0.3 # True
其实有很多法子解决浮点数计算失真的问题,并不一定非得使用 round 函数,比如我们可以通过作差比较两个浮点数是否相等,这样可以避免直接比较两个浮点数,而是比较他们的差的绝对值是否小于一个非常小的数。
(0.1 + 0.2) - 0.3 < 0.0001 # True
复数类型
.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 特有的嘛?
数值运算操作符
.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) 理解操作符:操作符是完成运算的一种符号体系
注意:/
和 //
操作符,它们和其它编程语言差异较大的地方
二元运算符:
数字类型的关系
- 类型间可进行混合运算,生成结果为”最宽”类型
- 三种类型存在一种逐渐”扩展”或”变宽”的关系:
整数 -> 浮点数 -> 复数
数值运算函数
.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)
单元小结
.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 个问题的求解。
认识使用变量的意义。
第四问中涉及到了函数的相关内容,函数会在第五周介绍,先对函数的写法和作用有个初步的认识即可。
问题分析
.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)
原来“好好学习,天天向上”这句话出处是……
第一问
.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)
dayup = pow(1.001, 365)
daydown = pow(0.999, 365)
print("向上:{:.2f},向下:{:.2f}".format(dayup, daydown))
# 向上:1.44,向下:0.69
第二问
.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)
dayfactor = ???
dayup = pow(1 + dayfactor, 365)
daydown = pow(1 - dayfactor, 365)
print("向上:{:.2f},向下:{:.2f}".format(dayup, daydown))
# dayfactor = 0.005
# 向上:6.17,向下:0.16
# dayfactor = 0.01
# 向上:37.78,向下:0.03
# dayfactor = 0.019
# 向上:962.89,向下:0.00
通过此 demo,体会变量的好处 —— 一处修改即可
第三问
.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)
dayup = 1.0
dayfactor = 0.01
for i in range(365):
if i % 7 in [6, 0]:
dayup = dayup * (1 - dayfactor)
else:
dayup = dayup * (1 + dayfactor)
print("工作日的力量:{:.2f} ".format(dayup))
# 工作日的力量:4.63
第四问
.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)
问:B 君在工作日需要每天进步多少,才能达到 A 君的效果呢?
def dayUP(df):
dayup = 1
for i in range(365):
if i % 7 in [6, 0]:
dayup = dayup * (1 - 0.01)
else:
dayup = dayup * (1 + df)
return dayup
dayfactor = 0.01
while dayUP(dayfactor) < 37.78: # 37.78 是一年每天努力 1% 的结果
dayfactor += 0.001
print("工作日的努力参数是:{:.3f} ".format(dayfactor))
# 工作日的努力参数是:0.019
:::success
现在时间:
看完本节后,突然想起昨晚看的一部番《跃动青春》里的一句台词……
:::
举一反三
.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。
字符串的格式化也是重点内容,不过细节有些多 —— “槽”的“格式控制标记”的写法。
可以结合老师的提供的若干示例来理解字符串的格式化,先理解课程中提供的示例代码。
单元开篇
.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)
字符串类型的表示
.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. 单引号
'字符串'
# 2. 双引号
"字符串"
# 3. 3 单引号
'''字符串'''
# 4. 3 双引号
"""字符串"""
字符串的序号:
字符串的使用:索引、切片
转义符:
其中 \r
在“实例 4:文本进度条”中会用到
字符串操作符
.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)
练习:
weekStr = "星期一星期二星期三星期四星期五星期六星期日"
weekId = eval(input("请输入星期数字(1-7):"))
pos = (weekId - 1) * 3
print(weekStr[pos : pos + 3])
"""
请输入星期数字(1-7):3
星期三
"""
weekStr = "一二三四五六日"
weekId = eval(input("请输入星期数字(1-7):"))
print("星期" + weekStr[weekId - 1])
"""
请输入星期数字(1-7):3
星期三
"""
字符串处理函数
.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
与 str 对应的是 eval 函数
Python 字符串的编码方式:Unicode 编码
Unicode 编码:
- 统一字符编码,即覆盖几乎所有字符的编码方式
- 从0到1114111 (0x10FFFF)空间,每个编码对应一个字符
- Python字符串中每个字符都是Unicode编码字符
print("1 + 1 = 2 " + chr(10004))
print("这个字符♉的Unicode值是:" + str(ord("♉")))
for i in range(12):
print(chr(9800 + i), end="")
"""
1 + 1 = 2 ✔
这个字符♉的Unicode值是:9801
♈♉♊♋♌♍♎♏♐♑♒♓
"""
:::warning
【补充说明】
准确点儿来说,这张图中提到的 Unicode 指的是“Unicode 码点值”
🤔 什么是“Unicode 编码”、“Unicode 码点值”呢?
- Unicode 编码:是一种字符编码方案,用于表示和处理字符
- Unicode 码点值:是字符在 Unicode 字符集中的唯一整数值
两者之间的关系:Unicode 编码定义了字符和对应的码点值之间的映射关系
我们可以这么理解:一个字符对应一个唯一的数字
chr()
可以根据我们给定的一个数字,找到对应的字符ord()
可以根据我们给定的一个字符,找到对应的数字
🤔 chr、ord 的全称是?
- chr 的全称是 character 表示字符
- ord 的全称是 ordinal 表示顺序、序数 :::
字符串处理方法
.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)
理解“方法”:
对于方法和函数的概念,在我看来都是一个意思,都是用函数调用符 ()
来调用的,没必要区分那么细。
字符串类型的格式化
.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(<逗号分隔的参数>)
- 槽
- 相当于一个占位符
- 使用一对大括号
{}
表示 - 仅在字符串中可用
print("{0:=^20}".format("PYTHON"))
# =======PYTHON=======
print("{0:*>20}".format("BIT"))
# *****************BIT
print("{:10}".format("BIT"))
# BIT
print("{0:,.2f}".format(12345.6789))
# 12,345.68
print("{0:b},{0:c},{0:d},{0:o},{0:x},{0:X}".format(425))
# 110101001,Ʃ,425,651,1a9,1A9
print("{0:e},{0:E},{0:f},{0:%}".format(3.14))
# 3.140000e+00,3.140000E+00,3.140000,314.000000%
单元小结
.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 文本进度条中加以应用。
单元开篇
.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 库基本介绍
.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 中处理时间的标准库
- 计算机时间的表达
- 提供获取系统时间并格式化输出功能
- 提供系统级精确计时功能,用于程序性能分析
import time
time.<b>()
小结:time 库包括三类函数
- 时间获取:
time()
ctime()
gmtime()
- 时间格式化:
strftime()
strptime()
- 程序计时:
sleep()
perf_counter()
时间获取
.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)
时间格式化
.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)
时间格式化:将时间以合理的方式展示出来
程序计时应用
.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()
其中 perf 是 performance(性能) 的缩写,perf_counter 可以理解为“性能测试”、“性能计时”。
理解了 perf_counter 的含义后,不难认识到它的作用:测量代码执行时间和性能的计时器。
上述两个 API:time.perf_counter() 和 time.sleep(s) 在接下来的文本进度条实例中会用到。
单元小结
.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: 文本进度条
问题分析
.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() 模拟一个持续的进度
“文本进度条”简单的开始
.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)
import time
scale = 10
print("------执行开始------")
for i in range(scale + 1):
a = "*" * i
b = "." * (scale - i)
c = (i / scale) * 100
print("{:^3.0f}%[{}->{}]".format(c, a, b))
time.sleep(0.1)
print("------执行结束------")
"""
------执行开始------
0 %[->..........]
10 %[*->.........]
20 %[**->........]
30 %[***->.......]
40 %[****->......]
50 %[*****->.....]
60 %[******->....]
70 %[*******->...]
80 %[********->..]
90 %[*********->.]
100%[**********->]
------执行结束------
"""
“文本进度条”单行动态刷新
.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)
import time
for i in range(101):
print("\r{:3}%".format(i), end="")
time.sleep(0.1)
# 最终的打印结果是从 0% 开始,每间隔 0.1s 自增 1%,逐步自增到 100%
“文本进度条”实例完整效果
.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)
import time
scale = 50
print("执行开始".center(scale // 2, "-"))
start = time.perf_counter()
for i in range(scale + 1):
a = "*" * i
b = "." * (scale - i)
c = (i / scale) * 100
dur = time.perf_counter() - start
print("\r{:^3.0f}%[{}->{}]{:.2f}s".format(c, a, b, dur), end="")
time.sleep(0.1)
print("\n" + "执行结束".center(scale // 2, "-"))
举一反三
.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)
练习
.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)
单选题
勘误:最后几个字应该描述的是:val 的“字符”个数,而非“数字”个数。
实例3:天天向上的力量
def dayUP(df):
dayup = 1
for i in range(365):
if i % 7 in [6,0]:
dayup = dayup*(1 - 0.01)
else:
dayup = dayup*(1 + df)
return dayup
dayfactor = 0.01
while dayUP(dayfactor) < 37.78:
dayfactor += 0.001
print("工作日的努力参数是: {:.3f}".format(dayfactor))
# 工作日的努力参数是: 0.019
这是本课程的实例3:
(1) 工作日模式,每天要努力到1.9%,相当于365模式每天1%的;
(2) 采用{:.3f}将输出数字变成三位小数点表示时,即使数学上该输出值是整数,也会按照小数方式输出。
实例4:文本进度条
import time
scale = 50
print("执行开始".center(scale // 2, "-"))
start = time.perf_counter()
for i in range(scale + 1):
a = "*" * i
b = "." * (scale - i)
c = (i / scale) * 100
dur = time.perf_counter() - start
print("\r{:^3.0f}%[{}->{}]{:.2f}s".format(c, a, b, dur), end="")
time.sleep(0.1)
print("\n" + "执行结束".center(scale // 2, "-"))
"""
-----------执行开始----------
100%[**************************************************->]5.20s
-----------执行结束----------
"""
这是本课程的实例4,请注意三点:
(1) 文本进度条程序使用了 perf_counter() 计时,计时方法适合各类需要统间的算问题,例如:比较不同算法时间 、统计程序运行时
(2) 进度条的单行回退要在命令行(也叫控制台、Windows的cmd)下才能使用,IDLE屏蔽了’\r’的功能
(3) 进度条可应用在:任何运行时间需要较长的程序中;任何希望提高用户体验的应中;进度条是人机交互的纽带之一
三次方格式化
a = eval(input())
print("{:-^20}".format(pow(a, 3)))
"""
10
--------1000--------
"""
这是一个简单题,重点在于理解格式化输出的方法。
星号三角形
n = eval(input())
for i in range(1, n + 1, 2):
print("{0:^{1}}".format("*" * i, n))
"""
3
*
***
5
*
***
*****
9
*
***
*****
*******
*********
"""
关键是对.format()中槽机制的理解,槽中可以嵌套槽,用来表示宽度、填充等含义。
凯撒密码
s = input()
t = ""
for c in s:
if "a" <= c <= "z":
t += chr(ord("a") + ((ord(c) - ord("a")) + 3) % 26)
elif "A" <= c <= "Z":
t += chr(ord("A") + ((ord(c) - ord("A")) + 3) % 26)
else:
t += c
print(t)
"""
python is good
sbwkrq lv jrrg
"""
恺撒密码加密算法由很多种编写方法,这是一种很直接的方法。
平方根格式化
a = eval(input())
print("{:+>30.3f}".format(pow(a, 0.5)))
"""
10
+++++++++++++++++++++++++3.162
"""
这是一个简单题,重点在于理解格式化输出的方法。
注意:如果平凡根后产生一个复数,由于复数的实部和虚部都是浮点数,.3f可以将实部和虚部分别取三位小数。
字符串分段组合
s = input()
ls = s.split("-")
print("{}+{}".format(ls[0], ls[-1]))
"""
Alice-Bob-Charis-David-Eric-Flurry
Alice+Flurry
"""
s.split(k) 以 k 为标记分割 s,产生一个列表。通过该题目,掌握 split() 方法的使用,注意:k 可以是单字符,也可以是字符串。
补充
计算机中小数计算存在不确定尾数的原因是什么?
计算机在计算浮点数时的失真问题
在学习不同的编程语言时,老师们普遍会提到的一个问题。以这篇文档记录一下自己对此的理解。
复数类型是 python 特有的嘛?
不是的,复数类型不是Python特有的。许多编程语言都支持复数类型,包括但不限于Python、C++、Java、Fortran、Ruby等。这些语言中,复数的表示和操作可能略有不同,但基本的概念是一致的。
在Python中,复数类型是内建的,可以直接创建和操作复数。例如:
x = 1 + 2j # 创建复数
y = 2 + 3j
z = x + y # 复数的加法
在其他一些语言中,如 C++ 或Java,可能需要通过特定的库或者使用对象来实现复数的功能。例如在 C++ 中,复数是通过 <complex>
库提供的 std::complex
类来实现的。
因此,虽然复数类型在很多语言中都有,但其使用的具体语法和实现方式会因语言而异。
Python 中的整数有范围限制吗?
Python 中的整数在理论上是没有大小限制的,只受限于你的机器的内存大小。
在大多数语言中,整数都是有固定的大小的,例如在 Java 或者 C++ 中,整数的大小一般是32位或64位。这意味着整数的大小有上限和下限,如果超过这个范围,就会发生溢出。
但在 Python 中,整数的大小是动态的,可以根据需要自动扩展。这使得 Python 非常适合于需要大整数计算的应用,例如密码学或者大数计算。
然而,这种灵活性也有一个代价。Python 的整数使用的内存比其他语言的整数多,因为它需要存储额外的信息来管理这种动态大小。而且,因为整数的大小可以无限扩展,如果你创建了一个非常大的整数,可能会消耗大量的内存,甚至导致你的程序崩溃。
所以,虽然 Python 的整数没有大小限制,但在实际应用中,你还是需要注意管理你的内存使用,避免创建过大的整数。