实例3:天天向上的力量
#DayDayUpQ1.py
dayup = pow(1.001,365)
daydown = pow(0.999,365)
print("向上: {:.2f},向下:{:.2f}".format(dayup,daydown))
#DayDayUpQ2.py
dayfactor = 0.005
dayup = pow(1+dayfactor,365)
daydown = pow(1-dayfactor,365)
print("向上: {:.2f},向下:{:.2f}".format(dayup,daydown))
dayfactor = 0.01
dayup = pow(1+dayfactor,365)
daydown = pow(1-dayfactor,365)
print("向上: {:.2f},向下:{:.2f}".format(dayup,daydown))
#DayDayUpQ3.py
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))
#DayDayUpQ4.py def保留字用于定义函数
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))
#round(x,d) 四舍五入函数 d截取位数,浮点型数据,有不确定尾数
#pow(x,y,z) == (x**y)%z
#x // y 整数除
字符串操作
#WeekNamePrintV1.py
weekStr = "星期一星期二星期三星期四星期五星期六星期日"
weekId = eval(input("请输入星期数字(1-7):"))
pos = (weekId - 1)*3
print(weekStr[pos:pos+3])
#WeekNamePrintV2.py
weekStr = "一二三四五六日"
weekId = eval(input("请输入星期数字(1-7):"))
print("星期"+weekStr[weekId-1])
'''
1.由一对单引号或双引号表示单行字符串
2.由一对三单引号或三双引号表示多行字符串
3,如果字符串中出现单引号,则需用双引,反之;如果出现单引号和双引号,则用三单引号
4.<字符串>[M:N],M缺失表示至开头,N缺失表示至结尾
<字符串>[M:N:K],根据步长K对字符串切片
<字符串>[::-1]字符串逆序
5.转义符
6.字符串格式化使用的方法:<模版字符串>.format(<逗号分隔的参数>)
槽{<参数序号>:<格式控制标记>}
'''
默认填充为空格,左对齐
time库的使用
time库是Python中处理时间的标准库:计算机时间表达、提供获取系统时间并格式化输出功能、提供系统级精确计时功能,用于程序性能分析
包括三类函数:
(1)时间获取:time() ctime() gmtime()
(2)时间格式化:strftime() strptime()
(3)程序计时:sleep() perf_counter()
实例4:文本进度条
#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("------执行结束------")
#TextProBarV2.py
import time
for i in range(101):
print("
{:3}%".format(i),end="")
time.sleep(0.1)
#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("
{:^3.0f}%[{}->{}]{:.2f}s".format(c,a,b,dur),end="")
time.sleep(0.1)
print("
"+"执行结束".center(scale//2,'-'))