Python语言程序设计(MOOC崇天)第三章基本数据类型学习笔记(天天向上的力量+文本进度条)

  • 回顾:

第一章

第二章

  • 本周内容:基本数据类型

 

 

  • 3.1数字类型及操作

整数类型:

浮点数

浮点数计算存在不确定尾数的原因.......

 

 

浮点数可以采用科学计数法表示

 

复数类型:

 

傅里叶变换用的多.....这里没有提到了。

 数值运算操作符

 

 

 

数值运算函数

 

 

  •  天天向上的力量
#daydayup
dayup = pow(1.001, 365)
daydown = pow(0.999, 365)
print("向上:{:.2f},向下:{:.2f}".format(dayup, daydown))

百分之1的力量

#daydayup
# 变量的好处
dayfactor = 0.005
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))

余数为0和6是周六和周天,可以这样算来得到。。。工作日的力量 

def dayup():
    dayfactor = 0.01
    day = pow(1+dayfactor, 365)
    return round(day, 2)
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
#
da = 0.01
c = dayup()
while dayUp(da) < c:
    da += 0.001
print("工作日的努力参数是:{:.3f}".format(da))

  •  字符串类型及操作

字符串表示

 字符串序号

字符串的使用

字符串切片的高级用法

字符串转义符的操作

 字符串操作符

 练习:

#WeekNamePrintV1

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

#WeekNamePrintV2

weekStr = "一二三四五六七"
weekId = eval(input("请输入星期数字(1-7):"))
print("星期"+weekStr[weekId-1])

  •  字符串处理函数

       这两个函数有什么用呢?内部运算是采用二进制完成,01构成,读取不方便,用十六、八进值来表示计算机内部的运算形式。通过这两个函数可以将计算机运算操作通过字符串打印出来,尤其是程序员关心的系统性程序非常有帮助。

     

知识点:1、这里的print有新知识:关于end=“ ”,为空则不换行、

              2、chr(x)后得到是字符故可以直接+“ 还是”

              3、ord(x)后得到是编码(是int)故不可以直接+“还是”,需要用eval()函数的对应功能的函数str()

字符串处理方法:

 方法必须要用.的方法执行

  •  字符串类型的格式化:

 

 

应该要去练习才行呀....... 

  • 模块二: time库

#字符串格式
import time as  t

print(t.time())

 

import time as  t

# print(t.time())

print(t.ctime())

import time as  t

# print(t.time())

# print(t.ctime())

print(t.gmtime())

 

  • 时间格式化

 

 

T = t.gmtime()
print(t.strftime("%Y-%m-%d %H:%M:%S", T))

 

  •  程序计时

 

  • 文本进度条

#TextProBarv1

import time as T
scale = 10
print("{0:*^20}".format("执行开始"))
for i in range(scale+1):
    a = '*'*i
    b = '.'*(scale -i)
    c = (i/scale)*100
    print("{:^3.0f}%[{}->{}]".format(c, a, b))
    T.sleep(0.1)
print("{0:*^20}".format("执行结束"))

import time as T
scale = 10
print("{0:*^20}".format("执行开始"))
for i in range(scale+1):
    a = '*'*i
    b = '.'*(scale -i)
    c = (i/scale)*100
    print("\r{:^3.0f}%[{}->{}]".format(c, a, b), end="")
    T.sleep(0.1)
#print默认是换行
print("")
print("{0:*^20}".format("执行结束"))

知识点:1.覆盖,print的end=“ ”

               2、光标自动到首行,用\r

 文本精度条单行动态刷新

#TextProBarv3
import time as T

scale = 50
print("执行开始".center(scale//2, "-"))
star = T.perf_counter()
for i in range(scale + 1):
    a = '*' * i
    b = '.' * (scale - i)
    c = (i/scale)*100
    dur = T.perf_counter() - star
    print("\r{:^3.0f}%[{}->{}]{:.2f}s".format(c, a, b, dur), end="")
    T.sleep(0.1)
print("\n"+"执行结束".center(scale//2, '-'))

虽然我可以用pycharm看到很好的效果,但是老师貌似说用IDE不太好,用控制台去看结果

 文本进度条举一反三:

作业题目:

 

x = input()
a = eval(x)
for i in range((a+1)//2):
    b = '*'*(i*2+1)
    # print(b.center(a)) 默认填充的是空格
    print((' '*((a-len(b))//2))+('*'*len(b))+(' '*((a-len(b))//2)))

u = input()
a = pow(eval(u), 3)
print("{0:-^20}".format(a))

 

#凯撒密码加密算法

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)

猜你喜欢

转载自blog.csdn.net/qq_37791134/article/details/83146850