1.try:错误处理
try: file=open('tt','r+') #尝试打开一个名为ttt的可读写文件 except Exception as e: #打不开时,将运行错误例外存到变量e # print(e) # No such file or directory: 'tt' print("No such file or directory: 'tt'") i = input("是否创建一个新文件(y or n)") if i == 'y': file = open ('tt','w') else: pass else: #打得开时 file.write('nihao') #在名为tt文件里面写入nihao file.close()2.zip、lambda、map
#zip 将列表的每一列取出来放一起 a = [1,2,3] b = [4,5,6] c = list(zip(a,b)) print(c) for i,j in zip(a,b): print(i/2,j*2) ========================== RESTART: D:\python\test1.py ========================= [(1, 4), (2, 5), (3, 6)] 0.5 8 1.0 10 1.5 12 #lambda 相当于一个函数,简化函数定义 fun = lambda x,y:x+y print(fun(1,2)) #map 快速调用,可以多输入,多输出 fun = lambda x,y:x+y c = map(fun,[1],[2]) #x=1,y=2 print(list(c)) d = map(fun,[1,2],[2,4]) #x=1,2;y=2,4;对应相加 print(list(d)) ========================== RESTART: D:\python\test1.py ========================= [3] [3, 6]3.copy和deepcopy
import copy a = [1,2,3] print(id(a)) #打印a的地址 b = a print(id(b)) c = copy.copy(a) #可能有些地址重复 print(c) print(id(c)) d = copy.deepcopy(a) #完全开辟另一个地址 print(d) ========================== RESTART: D:\python\test1.py ========================= 2754010421952 2754010421952 [1, 2, 3] 2754053231552 [1, 2, 3]4.threading 多线程
同时工作,节约时间
5.多核处理器
把任务平均分配给每一个核,每个核有单独的运算空间
6.tkinter
图像界面,交互界面
7.pickle 存放数据
#pickle 存放数据 import pickle d = {'da':111,2:[2,3,3],3:'aa'} file = open('yyyy.pickle','wb') #以pickle文件格式,wb:write bite,二进制写入 pickle.dump(d,file) #把d扔到file里 file.close #过段时间想重新编辑时 import pickle file = open('yyyy.pickle','rb') #rb:read bite e = pickle.load(file) file.close() print(e) #用 with 不需要关闭文件夹操作 with open('yyyy.pickle','rb') as file: e = pickle.load(file) print(e)8.set :去除相同
c = [2,'a',3,3,'a',6] d = {2,9,3,8,7} sentence = 'nihao,nn' print(set(c).difference(d)) #找出set(c)中和d不同元素 print(set(c).intersection(d)) ##找出set(c)中和d相同元素 print(set(c)) #set 将相同去除 print(set(sentence)) uc = set(c) uc.add(4) #add 添加4 #uc.clear() #清除 uc.remove(3) #不可以丢弃c中没有的元素 uc.discard(4) #可以丢弃c中没有的元素 print(uc)9.正则表达式:用于爬虫