python-第一块,笔记整理和学习内容复习(day4 - day5 - day6)
DAY4
学习内容
今天主要还是学习字典和集合的操作。根据这几天的学习写了第一次的作业--三级菜单
city_info = { '湖北':{'武汉':['武昌','汉口','汉阳'],'黄冈':['蕲春','罗田','英山','黄梅'],'荆州':['公安','石首','沙市']}, '安徽':{'合肥':['肥西','庐阳','蜀山'],'芜湖':['三山','鸠江','镜湖','弋江'],'安庆':['桐城','潜山','太湖']}, '江苏':{'南京':['建邺','雨花台','秦淮'],'苏州':['吴中','相城','平江','沧浪'],'无锡':['梁溪','滨湖','锡山']} } def open_meau: exit_flag = False while not exit_flag: for i in city_info: print(i) choose1 = input("please input your choosen1>>> ") if choose1 in city_info: while not exit_flag: for j in city_info[choose1]: print("\t",j) choose2 = input("please input your choosen2>>> ") if choose2 in city_info[choose1]: #while True: for k in city_info[choose1][choose2]: print("\t\t",k) choose_exit = input("This is the bottom,b to return to a layer>>>") if choose_exit == "b": pass elif choose_exit =="q": exit_flag = True elif choose2 == "b": break elif choose2 == "q": exit_flag = True else: print("your input was wrong,please input again") elif choose1 == "q": exit_flag = True else: print("your input was wrong,please input again")
随后更新了最新版
import yaml #导入yaml模块 f = open('city_info',"rb") dict_city = yaml.load(f) #利用yaml模块将城市信息文件转为多级字典 f.close() exit_flag = False #定义程序退出信息 while not exit_flag: for i in dict_city: #打印第一级城市信息 print(i) choose1 = input("please input your choosen1>>> ") #输入第二层城市选择 if choose1 in dict_city: while not exit_flag: for j in dict_city[choose1]: #打印第二层城市信息 print("\t",j) choose2 = input("please input your choosen2>>> ") #输入第三层城市选择 if choose2 in dict_city[choose1]: while not exit_flag: for k in dict_city[choose1][choose2]: #打印第三层城市信息 print("\t\t",k) choose3 =input("please input your choosen3>>> ") #输入第四层城市信息选择 if choose3 in dict_city[choose1][choose2]: for m in dict_city[choose1][choose2][choose3]: #打印第四层城市信息 print("\t\t\t",m) choose_exit = input("This is the bottom,b to return to a layer>>>") #打印菜单最底层信息 if choose_exit == "b": #返回上一层 pass elif choose_exit =="q": #退出程序 exit_flag = True elif choose3 =="b": #返回上一层 break elif choose3 =="q": #退出程序 exit_flag = True else: print("your input was wrong,please input again") elif choose2 == "b": #返回上一层 break elif choose2 == "q": #退出程序 exit_flag = True else: print("your input was wrong,please input again") elif choose1 == "q": #退出程序 exit_flag = True else: print("your input was wrong,please input again") #输入信息有误
笔记
集合
1、 .isdisjoint() #判断有无交集
2、 .add() #添加一项
.updata[] #添加多项
3、 .remove(" ") #删除
.discard(" ") #如果删除值不在,返回none 不会报错
.pop( ) #随机删除
list_1 = [1,4,5,7,3,6,7,9] list_1 = set(list_1) list_2 = set([2,6,0,66,22,8,4]) print(list_1,list_2,type(list_1)) #交集 list_1.intersection(list_2) print(list_1.intersection(list_2)) print(list_1 & list_2) print("---------------------") #并集 print(list_1.union(list_2)) print(list_1 | list_2) print("---------------------") #差集 print(list_1.difference(list_2)) print(list_1 - list_2) print(list_2.difference(list_1)) print(list_2 - list_1) print("---------------------") #对称差集 print(list_1.symmetric_difference(list_2)) print(list_1 ^ list_2) print("---------------------") #子集 print(list_1.issubset(list_2)) print(list_1.issuperset(list_2))
DAY5
学习内容
今天学习的主要是函数,写了一系列代码
def func1(): print("This is the func1") return 0 def func2(): print("This is the func2") def func3(): print("This is the func3") return 1,"hubei",["wa","jionu"],{"name":"zd"} x=func1() y=func2() z=func3() print(x) print(y) print(z) 全局变量和局部变量 school = "武汉轻工大学" def change_name(name): global school school = "湖北大学" print(name,school) change_name('董政宽') 递归 def clu(number): if int(number/2)>0: print(number) return clu(int(number/2)) clu(10)
DAY6
学习内容
今天学习了文件的一系列操作,有文件的读、写、追加等,详见代码
文件的读
f = open("today1","r+",encoding="utf-8") #list_f = f.readlines() #print(list_f) print(f.readline()) print(f.readline()) f.write("\n我爱北京天安门") f.write("\n北京天安门最伟大") f.flush() f.write("\n你好") for line in f.readlines(): print(line.strip()) for index,line in enumerate(f.readlines()): #不好的写法,只适于小文件,大文件不适用 if index == 1: print("----------------") print(line.strip()) count = 0 for line in f: if count == 2: print("--------------") count +=1 print(line.strip()) #高逼格写法,读一行,存一行 count += 1
文件的修改
f = open("today1","r",encoding="utf-8") f_new = open("today1_new","w",encoding="utf-8") for line in f: if "你好"in line: line = line.replace("你好","hello world") f_new.write(line) f.close() f_new.close() #修改文件代码
笔记
1、读 f = open("文件名","r")
f.read
2、写 f = open("文件名","w")
f.write("内容,\n")
3、关闭 f.close()
4、读一行 f.readline()
5、读出列表 f.readlines()
6、r+:读写模式
a:追加模式
a+:追加读
w+:写读,先创建文件再写
7、f.tell() #改变光标的位子
8、f.seek() #光标回到某个位子,默认是0
9、f.flush() #实时刷新
10、f.truncate() #不写,文件清空;写,从写的位子截断
11、with open("文件名") as f: #打开不用关闭,自动随程序关闭保存,可同时打开多个文件。
编码
python2.7 默认解码是bytes
python3.0 默认解码是unicode
unciode 万国编码,包含utf-8和gbk等
utf-8 可变长的编码扩展集,所以英文按ASCII存储占一个字节;所以中文占3个字节