第3周:基本数据类型
- 数据类型及操作
- 整数类型
pow(x,y)函数,计算x的y次方
四种进制的表达形式
- 浮点数类型
浮点数间运算存在不确定尾数,不是bug。
可以使用round(x,d)函数辅助
- 复数类型
x.real获得实部
x.imag获得虚部
- 数值运算操作符
abs()
divmod(x,y) 商余
pow(x,y[,z]) 幂余
- 数值运算函数
- 实例3:天天向上的力量
- 字符串类型及操作
- 字符串类型的表示
- 字符串操作符
- 字符串处理函数
hen(),oct(),chr(),ord()
- *字符串处理方法
str.lower(),srt.upper(),str.split(),str.count(),str.replace(),str.center(),str.strip(),str.jion()
- *字符串类型的格式化
format()
- 模块2:time库的使用
- time库基本介绍
标准函数
包含三类函数:
时间获取:time(),ctime(),gmtime()
时间格式化:strftime()
程序计时:perf_counter(),sleep()
- 时间获取
- 时间格式化
- 程序计时应用
- 实例4:文本进度条
#简单开始
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("-------执行结束--------")
#完整代码
#TextProBarV3.py
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,'-'))