week 3
本周学习了使用streamlit简单的网页设计,魔术方法、变量的类型注解,文件的相关操作。
streamlit可视化
照着代码做了一个简单的网页:
魔术方法
1.__str__将一个内存地址变成类对象输出
class Student:def __init__(self,name,age):self.name=nameself.age=agedef __str__(self):return f"Student类对象,name:{self.name},age:{self.age}"
stu=Student("周杰伦",31)
print(stu)
print(str(stu))
2.__lt__小于符号比较方法(大于也能比较)
def __lt__(self,other):return self.age<other.age
3.__le__小于等于比较方法(大于等于)
def __le__(self,other):return self.age<=other.age
4.__eq__比较运算符
class Student:def __init__(self,name,age):self.name=nameself.age=agedef __str__(self):return f"Student类对象,name:{self.name},age:{self.age}"def __lt__(self,other):return self.age==other.agedef __le__(self,other):return self.age==other.agedef __eq__(self,other):return self.age==other.age
stu_1=Student("周杰伦",31)
stu_2=Student("林俊杰",36)
print(stu_1==stu_2)
私有成员和方法只能内部使用,类中的其他成员可以访问私有成员
变量注解:
变量:类型
如:var_1:int =10
my_list: list[int]=[1,2,3]
var_1=random.randint(1,10) #type:int
文件
open函数可以打开一个已经存在的文件,或者创建一个新的文件(r)
open(name,mode,encoding)
mode:读(r),写(w),追加(a)。
encoding:编码格式
for循环读取文件换行:
关闭文件: f.close()
f=open("D:/test.txt","r",encoding="UTF-8")
for line in f:print(f"每一行数据是:{line}")
去除换行符:
f=open("D:/test.txt","r",encoding="UTF-8")
for line in f:line=line.strip()words=line.split(" ")print(words)
写文件(文件存在会把内容清空,重新写东西)f,close()自带f.flush功能。
f=open("D:/word.txt","w",encoding="UTF-8")
f.write("Hello World")
f.flush() #刷新
文件追加操作(a)(文件不存在会创建文件,文件存在会在后面追加写入文件内容)
通过pip下载安装包
开发可视化图表使用的技术栈:pyecharts包
如何打开官方画廊:https://gallery.pyecharts.org/#/README