本文记录第三周基本数据类型的学习过程
整数类型
整数无限制,最常用pow(x,y)函数,来计算x的y次幂 也可用x**y表示
二进制0b,0B开头
八进制0o,0O开头
十六进制0x,0X开头
浮点数类型
浮点数间运算存在不确定尾数,例如 0.1+0.2=0.30000000000000004
浮点数运算以及比较 利用round()函数辅助
round(x,d),对x四舍五入,保留d位小数
浮点数科学计数法表示
<a>e<b> 或<a>E<b>
4.3e-3 表示 4.3乘十的负三次方
数值运算函数
![](https://img-blog.csdnimg.cn/img_convert/32597cfc183228b189108b104363abad.png)
![](https://img-blog.csdnimg.cn/img_convert/ca6f3cdb893b7404c0ccedb0a2e84ac6.png)
![](https://img-blog.csdnimg.cn/img_convert/440e92dc0e4df7fd922f16338d0f5ba5.png)
天天向上的力量
一 每一天
dayfactor=eval(input())
dayup=pow(1+dayfactor,365)
daydown=pow(1-dayfactor,365)
print("向上:{:.2f},向下:{:.2f}".format(dayup,daydown))
二 工作日
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)
i%7 余数为0或6 即为周末,其他为工作日
三 工作日 VS 每一天
#DayDayUpQ4.py
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))
def保留字为定义函数,其中df为参数
字符串
字符串的序号
![](https://img-blog.csdnimg.cn/img_convert/aa6a22da7a0eadcaae465fd1691dfa2e.png)
字符串的使用
![](https://img-blog.csdnimg.cn/img_convert/fa18d3a8411cfbccb7ac126ab35db531.png)
![](https://img-blog.csdnimg.cn/img_convert/296ec278c8aed69e6e6855e53cad2e2b.png)
[::-1]可以切片实现逆序
特殊字符
![](https://img-blog.csdnimg.cn/img_convert/2a44f9b4b39f832e4865bc02e26ad1f5.png)
字符串操作符
![](https://img-blog.csdnimg.cn/img_convert/217eaa38ff07b250b12d573edce3e846.png)
获取星期字符串
weekstr="一二三四五六日"
weekId=eval(input())
print('星期'+weekstr[weekId-1])
字符串处理函数
![](https://img-blog.csdnimg.cn/img_convert/9c1dc778160dcfb1fa997f5548ac06e6.png)
![](https://img-blog.csdnimg.cn/img_convert/52b5c8e452b6631d040746ce8014701b.png)
字符串处理方法
![](https://img-blog.csdnimg.cn/img_convert/3868755a20298d117c2217f4bba17708.png)
![](https://img-blog.csdnimg.cn/img_convert/c6b388dcaee5df37db8e2b807a24e74b.png)
![](https://img-blog.csdnimg.cn/img_convert/0fded238ab4b8127dcfde0379c430e1c.png)
字符串格式化
字符串格式化使用format函数,使用方法如下
<模板字符串>.format(参数)
![](https://img-blog.csdnimg.cn/img_convert/9d5437b9c807a1bbc102bb07325bd6fc.png)
![](https://img-blog.csdnimg.cn/img_convert/dea84c27ce6bc4bb7549e2b75abe3e25.png)
![](https://img-blog.csdnimg.cn/img_convert/e94beda75903bd56a5368b0d762c9a5b.png)
time库的使用
时间获取
![](https://img-blog.csdnimg.cn/img_convert/272d7698a39b2a295778bbe6017721ad.png)
![](https://img-blog.csdnimg.cn/img_convert/295bbf8377535ccafbd9c240f57c8c52.png)
时间格式化
![](https://img-blog.csdnimg.cn/img_convert/b4461db55e745aba3845b06e492256fd.png)
![](https://img-blog.csdnimg.cn/img_convert/286e4385a15258e8a0d52ea0e24a45be.png)
![](https://img-blog.csdnimg.cn/img_convert/0fc0d77cb46ab94ecdd6334695a88b4c.png)
![](https://img-blog.csdnimg.cn/img_convert/ad7079b6a0b12025ccfb184dfa4c4fd9.png)
计时应用
![](https://img-blog.csdnimg.cn/img_convert/cc8ac720f5137d26e2b1205918514696.png)
![](https://img-blog.csdnimg.cn/img_convert/80dd83168c36981cf3ac15cf8e0befce.png)
应用——文本进度条
1.较为简单的进度条
#TextProBarV1.py
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("------执行结束------")
2.单行刷新进度条
#TextProBarV2.py
import time
for i in range(101):
print("\r{:3}%".format(i), end="")
time.sleep(0.1)
\r 转义字符为不换行输出,达到刷新效果
3.实例
#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,'-'))